Debian Codegraph @

apps/converters/ivdowngrade/SoLODV2.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*
38 * Copyright (C) 1995-96 Silicon Graphics, Inc.
39 *
40 _______________________________________________________________________
41 ______________ S I L I C O N G R A P H I C S I N C . ____________
42 |
43 | Classes:
44 | SoLODV2
45 |
46 | Authors: David Mott
47 |
48 ______________ S I L I C O N G R A P H I C S I N C . ____________
49 _______________________________________________________________________
50 */
51
52 #include <cmath>
53 #include <float.h>
54 #include <Inventor/nodes/SoLevelOfDetail.h>
55 #include <Inventor/actions/SoGetMatrixAction.h>
56 #include <Inventor/actions/SoGetBoundingBoxAction.h>
57 #include <Inventor/actions/SoWriteAction.h>
58 #include "SoLODV2.h"
59
60 SO_NODE_SOURCE(SoLODV2);
61
62 ////////////////////////////////////////////////////////////////////////
63 //
64 // Description:
65 // init that class!
66 //
67 void
68 SoLODV2::initClass()
69 //
70 ////////////////////////////////////////////////////////////////////////
71 {
72 SO__NODE_INIT_CLASS(SoLODV2, "LOD", SoLOD);
73
74 // Tell the type system whenever it wants to create something
75 // of type SoLOD to create a SoLODV2
76 SoType::overrideType(
77 SoLOD::getClassTypeId(), &SoLODV2::createInstance);
78 }
79
80 ////////////////////////////////////////////////////////////////////////
81 //
82 // Description:
83 // Constructor
84 //
85 SoLODV2::SoLODV2()
86 //
87 ////////////////////////////////////////////////////////////////////////
88 {
89 SO_NODE_CONSTRUCTOR(SoLODV2);
90 SO_NODE_ADD_FIELD(alternateRep, (NULL));
91 }
92
93 ////////////////////////////////////////////////////////////////////////
94 //
95 // Description:
96 // Destructor
97 //
98 SoLODV2::~SoLODV2()
99 //
100 ////////////////////////////////////////////////////////////////////////
101 {
102
103 }
104
105 ////////////////////////////////////////////////////////////////////////
106 //
107 // Description:
108 // write the alternate rep for this node
109 //
110 void
111 SoLODV2::write(SoWriteAction *action)
112 //
113 ////////////////////////////////////////////////////////////////////////
114 {
115 SoOutput *out = action->getOutput();
116
117 // In write-reference counting phase
118 if (out->getStage() == SoOutput::COUNT_REFS) {
119 // Only do this once!
120 if (alternateRep.isDefault()) {
121 SoLevelOfDetail *levelOfDetail = new SoLevelOfDetail;
122 alternateRep = levelOfDetail;
123
124 // All LOD children become LevelOfDetail children
125 int j;
126 for (j=0; j < getNumChildren(); j++)
127 levelOfDetail->addChild(getChild(j));
128
129 // We don't need children here any more - they are in the alternateRep
130 removeAllChildren();
131
132 // Convert the LOD ranges to LevelOfDetail screen areas
133 // We reverse the process of ivToVRML's convertLevelOfDetailToLOD():
134
135 // Assuming a screen height and width of 1024 and a 45 degree field
136 // of view, the formula for converting a screenArea to a distance
137 // is:
138 //
139 // (1024 / 2) * worldBboxRadius
140 // distance = ______________________________________
141 //
142 // tan( 45 / 2) * sqrt(screenArea / PI)
143 //
144
145 // So, converting distance to screenArea is:
146 //
147 // (1024/2) * worldBboxRadius
148 // f = --------------------------------
149 // tan( 45 / 2 ) * distance
150 //
151 //
152 // screenArea = f^2 * PI
153 //
154
155 const SoPath *path = action->getCurPath();
156
157 // get the matrix
158 SoGetMatrixAction matAction(SbViewportRegion(1024, 1024));
159
160 matAction.apply((SoPath *) path); // const cast away!
161
162 SbMatrix toWorld = matAction.getMatrix();
163
164 // transform the bbox to world space
165 SoGetBoundingBoxAction bba(SbViewportRegion(100, 100));
166 bba.apply(levelOfDetail);
167 SbBox3f bbox = bba.getBoundingBox();
168
169 bbox.transform(toWorld);
170
171 float w,h,d;
172 bbox.getSize(w,h,d);
173
174 w *= 0.5;
175 h *= 0.5;
176 d *= 0.5;
177
178 float radius = sqrt(w*w + h*h + d*d);
179
180 // empirically, half the radius seems to work better
181 radius *= 0.5;
182
183 float distance, newScreenArea;
184 float tanHalf45 = tan((0.5 * 45.0 / 180.0) * M_PI);
185
186 for (j=0; j < range.getNum(); j++) {
187 distance = range[j];
188 if (distance != 0.0) {
189 newScreenArea = 512.0 * radius / (tanHalf45 * distance);
190 newScreenArea = newScreenArea * newScreenArea * M_PI;
191 } else {
192 newScreenArea = FLT_MAX;
193 }
194 levelOfDetail->screenArea.set1Value(j, newScreenArea);
195 }
196 }
197 }
198
199 SoLOD::write(action);
200 }
201