Equalizer  1.6.1
sliceClipping.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 "sliceClipping.h"
30 
31 
32 namespace eVolve
33 {
34 
35  const int SliceClipper::nSequence[8][8] = {
36  {7,3,5,6,1,2,4,0},
37  {6,2,4,7,0,3,5,1},
38  {5,1,4,7,0,3,6,2},
39  {4,0,5,6,1,2,7,3},
40  {3,1,2,7,0,5,6,4},
41  {2,0,3,6,1,4,7,5},
42  {1,0,3,5,2,4,7,6},
43  {0,1,2,4,3,5,6,7},
44  };
45 
46  const float SliceClipper::sequence[64] = {
47  0, 1, 4, 2, 3, 5, 6, 7,
48  1, 0, 3, 5, 4, 2, 7, 6,
49  2, 0, 6, 3, 1, 4, 7, 5,
50  3, 1, 2, 7, 5, 0, 6, 4,
51  4, 0, 5, 6, 2, 1, 7, 3,
52  5, 1, 7, 4, 0, 3, 6, 2,
53  6, 2, 4, 7, 3, 0, 5, 1,
54  7, 3, 6, 5, 1, 2, 4, 0 };
55 
56  const float SliceClipper::v1[24] = {
57  0, 1, 4, 4,
58  1, 0, 1, 4,
59  0, 2, 5, 5,
60  2, 0, 2, 5,
61  0, 3, 6, 6,
62  3, 0, 3, 6 };
63 
64  const float SliceClipper::v2[24] = {
65  1, 4, 7, 7,
66  5, 1, 4, 7,
67  2, 5, 7, 7,
68  6, 2, 5, 7,
69  3, 6, 7, 7,
70  4, 3, 6, 7 };
71 
72 
73 void SliceClipper::updatePerFrameInfo
74 (
75  const eq::Matrix4d& modelviewM,
76  const eq::Matrix3d& modelviewITM,
77  const double newSliceDistance,
78  const eq::Range& range
79 )
80 {
81  double zRs = -1+2.*range.start;
82  double zRe = -1+2.*range.end;
83 
84  //rendering parallelepipid's verteces
85  eq::Vector4d vertices[8];
86  vertices[0] = eq::Vector4d(-1.0,-1.0,zRs, 1.0);
87  vertices[1] = eq::Vector4d( 1.0,-1.0,zRs, 1.0);
88  vertices[2] = eq::Vector4d(-1.0, 1.0,zRs, 1.0);
89  vertices[3] = eq::Vector4d( 1.0, 1.0,zRs, 1.0);
90 
91  vertices[4] = eq::Vector4d(-1.0,-1.0,zRe, 1.0);
92  vertices[5] = eq::Vector4d( 1.0,-1.0,zRe, 1.0);
93  vertices[6] = eq::Vector4d(-1.0, 1.0,zRe, 1.0);
94  vertices[7] = eq::Vector4d( 1.0, 1.0,zRe, 1.0);
95 
96  for( int i=0; i<8; i++ )
97  for( int j=0; j<3; j++)
98  shaderVertices[ i*3+j ] = float( vertices[i][j] );
99 
100 
101  this->viewVec = eq::Vector4d( -modelviewM.array[ 2],
102  -modelviewM.array[ 6],
103  -modelviewM.array[10],
104  0.0 );
105 
106  viewVecf = eq::Vector3f( float( viewVec.x( )), float( viewVec.y( )),
107  float( viewVec.z( )));
108 
109  sliceDistance = newSliceDistance;
110 
111  frontIndex = 0;
112  float maxDist = float( viewVec.dot( vertices[0] ));
113  for( int i = 1; i < 8; i++ )
114  {
115  const float dist = float( viewVec.dot( vertices[i] ));
116  if ( dist > maxDist)
117  {
118  maxDist = dist;
119  frontIndex = i;
120  }
121  }
122 
123  planeStart = viewVec.dot( vertices[nSequence[frontIndex][0]] );
124  double dS = ceil( planeStart/sliceDistance );
125  planeStart = dS * sliceDistance;
126 }
127 
128 
129 eq::Vector3f SliceClipper::getPosition
130 (
131  const int vertexNum,
132  const int sliceNum
133 ) const
134 {
135  const float dPlaneDist = float( planeStart + sliceNum * sliceDistance );
136  float3 Position = float3( 0.0, 0.0, 0.0 );
137 
138  for( int e = 0; e < 4; e++ )
139  {
140  int vidx1 = 3*static_cast<int>( sequence[
141  static_cast<int>(frontIndex * 8 + v1[vertexNum*4+e])]);
142  int vidx2 = 3*static_cast<int>( sequence[
143  static_cast<int>(frontIndex * 8 + v2[vertexNum*4+e])]);
144 
145  float3 vecV1( shaderVertices[vidx1 ],
146  shaderVertices[vidx1+1],
147  shaderVertices[vidx1+2] );
148 
149  float3 vecV2( shaderVertices[vidx2 ],
150  shaderVertices[vidx2+1],
151  shaderVertices[vidx2+2] );
152 
153  float3 vecStart = vecV1;
154  float3 vecDir = vecV2-vecV1;
155 
156  float denom = vecDir.dot( viewVecf );
157  float lambda = (denom != 0.0f) ?
158  (dPlaneDist - vecStart.dot(viewVecf))/denom : -1.0f;
159 
160  if(( lambda >= 0.0f ) && ( lambda <= 1.0f ))
161  {
162  Position = vecStart + vecDir * lambda;
163  break;
164  }
165  }
166  return Position;
167 }
168 
169 
170 }
171