Debian Codegraph @

src/scene/x3d/x3dnodes_verticesandtrianglescounting.inc

raw
1 {%MainUnit x3dnodes.pas}
2 {
3 Copyright 2003-2023 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" 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.
13
14 ----------------------------------------------------------------------------
15 }
16
17 { implementacja metod VerticesCount i TrianglesCount dla specyficznych
18 potomkow TAbstractGeometryNode }
19
20 function TAbstractGeometryNode.CallProxyVerticesCount(
21 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
22 begin
23 Result := ProxyGeometry.VerticesCount(ProxyState, nil, nil);
24 end;
25
26 function TAbstractGeometryNode.VerticesCount(
27 State: TX3DGraphTraverseState;
28 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
29 var
30 C: TMFVec3f;
31 begin
32 if InternalCoord(State, C) then
33 begin
34 if C <> nil then
35 Result := C.Count else
36 Result := 0;
37 end else
38 begin
39 if ProxyGeometry <> nil then
40 Result := CallProxyVerticesCount(ProxyGeometry, ProxyState) else
41 raise EInternalError.CreateFmt('%s: TAbstractGeometryNode.VerticesCount not overridden, and node not coordinate-based and without a Proxy', [ClassName]);
42 end;
43 end;
44
45 function TAbstractGeometryNode.CallProxyTrianglesCount(
46 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
47 begin
48 Result := ProxyGeometry.TrianglesCount(ProxyState, nil, nil);
49 end;
50
51 function TAbstractGeometryNode.TrianglesCount(
52 State: TX3DGraphTraverseState;
53 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
54 begin
55 if ProxyGeometry <> nil then
56 Result := CallProxyTrianglesCount(ProxyGeometry, ProxyState) else
57 raise EInternalError.CreateFmt('%s: TAbstractGeometryNode.TrianglesCount not overridden, and node without a Proxy', [ClassName]);
58 end;
59
60 function Cone_TrianglesCount(State: TX3DGraphTraverseState;
61 const Sides, Bottom: boolean; const Slices, Stacks: Cardinal): Cardinal;
62 begin
63 Result := 0;
64 if Sides then
65 Result := Result + (Slices * ((Stacks - 1) * 2 + 1));
66 if Bottom then
67 Result := Result + Slices;
68 end;
69
70 {$warnings off} // using deprecated to keep supporting VRML 1
71 function TConeNode_1.TrianglesCount(State: TX3DGraphTraverseState;
72 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
73 begin
74 Result := Cone_TrianglesCount(State,
75 FdParts.Flags[CONE_PARTS_SIDES], FdParts.Flags[CONE_PARTS_BOTTOM],
76 DefaultTriangulationSlices, DefaultTriangulationStacks);
77 end;
78 {$warnings on}
79
80 function TConeNode.TrianglesCount(State: TX3DGraphTraverseState;
81 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
82 begin
83 Result := Cone_TrianglesCount(State,
84 FdSide.Value, FdBottom.Value, CalculateSlices, CalculateStacks);
85 end;
86
87 function Box_TrianglesCount(State: TX3DGraphTraverseState): Cardinal;
88 begin
89 result := 6 * 2;
90 end;
91
92 function TCubeNode_1.TrianglesCount(State: TX3DGraphTraverseState;
93 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
94 begin
95 Result := Box_TrianglesCount(State);
96 end;
97
98 function TBoxNode.TrianglesCount(State: TX3DGraphTraverseState;
99 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
100 begin
101 Result := Box_TrianglesCount(State);
102 end;
103
104 function Cylinder_TrianglesCount(State: TX3DGraphTraverseState;
105 const Bottom, Side, Top: boolean; const Slices, Stacks: Cardinal): Cardinal;
106 begin
107 Result := 0;
108 if Side then
109 Result := Result + (2 * Slices * Stacks);
110 if Top then
111 Result := Result + Slices;
112 if Bottom then
113 Result := Result + Slices;
114 end;
115
116 {$warnings off} // using deprecated to keep supporting VRML 1
117 function TCylinderNode_1.TrianglesCount(State: TX3DGraphTraverseState;
118 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
119 begin
120 Result := Cylinder_TrianglesCount(State,
121 FdParts.Flags[CYLINDER_PARTS_BOTTOM],
122 FdParts.Flags[CYLINDER_PARTS_SIDES],
123 FdParts.Flags[CYLINDER_PARTS_TOP],
124 DefaultTriangulationSlices, 1 { no stacks used for cylinder });
125 end;
126 {$warnings on}
127
128 function TCylinderNode.TrianglesCount(State: TX3DGraphTraverseState;
129 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
130 begin
131 Result := Cylinder_TrianglesCount(State,
132 FdBottom.Value, FdSide.Value, FdTop.Value, CalculateSlices, CalculateStacks);
133 end;
134
135 function IndexedPolygons_TrianglesCount(
136 State: TX3DGraphTraverseState;
137 CoordIndex: TMFLong; Coord: TMFVec3f): Cardinal;
138 var
139 BeginIndex, EndIndex: integer;
140 begin
141 if Coord = nil then
142 Exit(0);
143
144 BeginIndex := 0;
145 result := 0;
146 while BeginIndex < CoordIndex.Count do
147 begin
148 EndIndex := BeginIndex;
149 while (EndIndex < CoordIndex.Count) and
150 (CoordIndex.Items.L[EndIndex] <> -1) do
151 Inc(EndIndex);
152
153 result := result + (Cardinal(Max(EndIndex - BeginIndex - 2, 0)));
154 BeginIndex := EndIndex + 1;
155 end;
156 end;
157
158 function TIndexedFacesOrTrianglesNode_1.TrianglesCount(State: TX3DGraphTraverseState;
159 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
160 begin
161 Result := IndexedPolygons_TrianglesCount(State,
162 FdCoordIndex, InternalCoordinates(State));
163 end;
164
165 function TIndexedFaceSetNode.TrianglesCount(State: TX3DGraphTraverseState;
166 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
167 begin
168 Result := IndexedPolygons_TrianglesCount(State,
169 FdCoordIndex, InternalCoordinates(State));
170 end;
171
172 function TIndexedLineSetNode_1.TrianglesCount(State: TX3DGraphTraverseState;
173 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
174 begin
175 result := 0;
176 end;
177
178 function TIndexedLineSetNode.TrianglesCount(State: TX3DGraphTraverseState;
179 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
180 begin
181 result := 0;
182 end;
183
184 function TLineSetNode.TrianglesCount(State: TX3DGraphTraverseState;
185 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
186 begin
187 result := 0;
188 end;
189
190 function TPointSetNode_1.TrianglesCount(State: TX3DGraphTraverseState;
191 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
192 begin
193 result := 0;
194 end;
195
196 function TPointSetNode.TrianglesCount(State: TX3DGraphTraverseState;
197 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
198 begin
199 result := 0;
200 end;
201
202 function Sphere_TrianglesCount(State: TX3DGraphTraverseState;
203 const Slices, Stacks: Cardinal): Cardinal;
204 begin
205 result := Slices * ((Stacks - 2) * 2 + 2);
206 end;
207
208 function TSphereNode_1.TrianglesCount(State: TX3DGraphTraverseState;
209 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
210 begin
211 Result := Sphere_TrianglesCount(State,
212 DefaultTriangulationSlices, DefaultTriangulationStacks);
213 end;
214
215 function TSphereNode.TrianglesCount(State: TX3DGraphTraverseState;
216 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
217 begin
218 Result := Sphere_TrianglesCount(State,
219 CalculateSlices, CalculateStacks);
220 end;
221
222 { TElevationGridNode --------------------------------------------------------- }
223
224 function TElevationGridNode.TrianglesCount(
225 State: TX3DGraphTraverseState;
226 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
227 begin
228 if IsNotEmpty then
229 Result := (FdXDimension.Value - 1) * (FdZDimension.Value - 1) * 2 else
230 Result := 0;
231 end;
232
233 function TElevationGridNode.VerticesCount(
234 State: TX3DGraphTraverseState;
235 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
236 begin
237 if IsNotEmpty then
238 Result := FdXDimension.Value * FdZDimension.Value else
239 Result := 0;
240 end;
241
242 { TExtrusionNode ------------------------------------------------------------- }
243
244 function TExtrusionNode.TrianglesCount(
245 State: TX3DGraphTraverseState;
246 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
247 var
248 E: TVRMLExtrusion;
249 begin
250 E := TVRMLExtrusion.Create;
251 try
252 E.Node := Self;
253 Result := (E.High + 1) * (FdCrossSection.Count - 1) * 2;
254 if FdCrossSection.Count >= 2 then
255 begin
256 if FdBeginCap.Value then
257 Result := Result + (Max(FdCrossSection.Count - 2 - Integer(E.CrossSectionOmit), 0));
258 if FdEndCap.Value then
259 Result := Result + (Max(FdCrossSection.Count - 2 - Integer(E.CrossSectionOmit), 0));
260 end;
261 finally FreeAndNil(E) end;
262 end;
263
264 { X3D [Indexed] Triangle/Quad Set/Strip/Fan ---------------------------------- }
265
266 function TIndexedTriangleSetNode.TrianglesCount(
267 State: TX3DGraphTraverseState;
268 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
269 begin
270 if InternalCoordinates(State) <> nil then
271 Result := CoordIndexField.Count div 3 else
272 { If Coord is non-nil but has not enough items as indexed by
273 CoordIndex, it's invalid according to X3D spec.
274 So we don't worry about this case. }
275 Result := 0;
276 end;
277
278 function TTriangleSetNode.TrianglesCount(
279 State: TX3DGraphTraverseState;
280 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
281 var
282 ACoord: TMFVec3f;
283 begin
284 ACoord := InternalCoordinates(State);
285 if ACoord <> nil then
286 Result := ACoord.Count div 3 else
287 Result := 0;
288 end;
289
290 function TIndexedTriangleFanSetNode.TrianglesCount(
291 State: TX3DGraphTraverseState;
292 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
293 begin
294 Result := IndexedPolygons_TrianglesCount(State,
295 FdIndex, InternalCoordinates(State));
296 end;
297
298 function TTriangleFanSetNode.TrianglesCount(
299 State: TX3DGraphTraverseState;
300 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
301 begin
302 if InternalCoordinates(State) <> nil then
303 { For each fan, add it's count - 2. }
304 Result := FdFanCount.Items.Sum - FdFanCount.Items.Count * 2 else
305 Result := 0;
306 end;
307
308 function TIndexedTriangleStripSetNode.TrianglesCount(
309 State: TX3DGraphTraverseState;
310 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
311 begin
312 Result := IndexedPolygons_TrianglesCount(State,
313 FdIndex, InternalCoordinates(State));
314 end;
315
316 function TTriangleStripSetNode.TrianglesCount(
317 State: TX3DGraphTraverseState;
318 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
319 begin
320 if InternalCoordinates(State) <> nil then
321 { For each strip, add it's count - 2. }
322 Result := FdStripCount.Items.Sum - FdStripCount.Items.Count * 2 else
323 Result := 0;
324 end;
325
326 function TIndexedQuadSetNode.TrianglesCount(
327 State: TX3DGraphTraverseState;
328 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
329 begin
330 if InternalCoordinates(State) <> nil then
331 Result := 2 * (FdIndex.Count div 4) else
332 { If Coord is non-nil but has not enough items as indexed by
333 CoordIndex, it's invalid according to X3D spec.
334 So we don't worry about this case. }
335 Result := 0;
336 end;
337
338 function TQuadSetNode.TrianglesCount(
339 State: TX3DGraphTraverseState;
340 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): Cardinal;
341 var
342 ACoord: TMFVec3f;
343 begin
344 ACoord := InternalCoordinates(State);
345 if ACoord <> nil then
346 Result := 2 * (ACoord.Count div 4) else
347 Result := 0;
348 end;
349
350 { ---------------------------------------------------------------------------- }