Equalizer  1.6.1
framesOrderer.cpp
1 
2 /* Copyright (c) 2007 Maxim Makhinya
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * - Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * - Neither the name of Eyescale Software GmbH nor the names of its
13  * contributors may be used to endorse or promote products derived from this
14  * software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "framesOrderer.h"
30 
31 namespace eVolve
32 {
33 
34 static bool cmpRangesDec(const eq::Frame* frame1, const eq::Frame* frame2)
35 {
36  return frame1->getRange().start < frame2->getRange().start;
37 }
38 
39 
40 static bool cmpRangesInc(const eq::Frame* frame1, const eq::Frame* frame2)
41 {
42  return frame1->getRange().start > frame2->getRange().start;
43 }
44 
45 
46 void orderFrames( eq::Frames& frames, const eq::Matrix4d& modelviewM,
47  const eq::Matrix3d& modelviewITM,
48  const eq::Matrix4f& rotation, const bool orthographic )
49 {
50  if( orthographic )
51  {
52  const bool orientation = rotation.array[10] < 0;
53  sort( frames.begin(), frames.end(),
54  orientation ? cmpRangesInc : cmpRangesDec );
55  return;
56  }
57  // else perspective projection
58 
59  eq::Vector3d norm = modelviewITM * eq::Vector3d( 0.0, 0.0, 1.0 );
60  norm.normalize();
61 
62  sort( frames.begin(), frames.end(), cmpRangesInc );
63 
64  // cos of angle between normal and vectors from center
65  std::vector<double> dotVals;
66 
67  // of projection to the middle of slices' boundaries
68  for( eq::Frames::const_iterator i = frames.begin();
69  i != frames.end(); ++i )
70  {
71  const eq::Frame* frame = *i;
72  const double px = -1.0 + frame->getRange().end*2.0;
73 
74  const eq::Vector4d pS = modelviewM * eq::Vector4d( 0.0, 0.0, px , 1.0 );
75  eq::Vector3d pSsub( pS[ 0 ], pS[ 1 ], pS[ 2 ] );
76  pSsub.normalize();
77  dotVals.push_back( norm.dot( pSsub ));
78  }
79 
80  const eq::Vector4d pS = modelviewM * eq::Vector4d( 0.0, 0.0,-1.0, 1.0 );
81  eq::Vector3d pSsub( pS[ 0 ], pS[ 1 ], pS[ 2 ] );
82  pSsub.normalize();
83  dotVals.push_back( norm.dot( pSsub ));
84  //check if any slices need to be rendered in reverse order
85  size_t minPos = std::numeric_limits< size_t >::max();
86  for( size_t i=0; i<dotVals.size()-1; i++ )
87  if( dotVals[i] > 0 && dotVals[i+1] > 0 )
88  minPos = static_cast< int >( i );
89 
90  const size_t nFrames = frames.size();
91  minPos++;
92  if( minPos < frames.size()-1 )
93  {
94  eq::Frames framesTmp = frames;
95 
96  // copy slices that should be rendered first
97  memcpy( &frames[ nFrames-minPos-1 ], &framesTmp[0],
98  (minPos+1)*sizeof( eq::Frame* ) );
99 
100  // copy slices that should be rendered last, in reverse order
101  for( size_t i=0; i<nFrames-minPos-1; i++ )
102  frames[ i ] = framesTmp[ nFrames-i-1 ];
103  }
104 }
105 
106 }
107 
const Range & getRange() const
A holder for a frame data and related parameters.
Definition: client/frame.h:42
std::vector< Frame * > Frames
A vector of pointers to eq::Frame.