KiCad PCB EDA Suite
Loading...
Searching...
No Matches
vrml2_box.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2016 Cirilo Bernardo <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21
22#include <iostream>
23#include <sstream>
24#include <wx/log.h>
25
26#include "vrml2_base.h"
27#include "vrml2_box.h"
29
30
32{
34 size.x = 2.0;
35 size.y = 2.0;
36 size.z = 2.0;
37}
38
39
41{
43 m_Parent = aParent;
44 size.x = 2.0;
45 size.y = 2.0;
46 size.z = 2.0;
47
48 if( nullptr != m_Parent )
49 m_Parent->AddChildNode( this );
50}
51
52
54{
55 wxLogTrace( traceVrmlPlugin, wxT( " * [INFO] Destroying Box node." ) );
56}
57
58
60{
61 // this node is dangling unless it has a parent of type WRL2_SHAPE
62
63 if( nullptr == m_Parent || m_Parent->GetNodeType() != WRL2NODES::WRL2_SHAPE )
64 return true;
65
66 return false;
67}
68
69
70bool WRL2BOX::Read( WRLPROC& proc, WRL2BASE* aTopNode )
71{
72 char tok = proc.Peek();
73
74 if( proc.eof() )
75 {
76 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
77 " * [INFO] bad file format; unexpected eof %s." ),
78 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition() );
79
80 return false;
81 }
82
83 if( '{' != tok )
84 {
85 wxLogTrace( traceVrmlPlugin,
86 wxT( "%s:%s:%d\n"
87 " * [INFO] bad file format; expecting '{' but got '%s' %s." ),
88 __FILE__, __FUNCTION__, __LINE__, tok, proc.GetFilePosition() );
89
90 return false;
91 }
92
93 proc.Pop();
94 std::string glob;
95
96 if( proc.Peek() == '}' )
97 {
98 proc.Pop();
99 return true;
100 }
101
102 if( !proc.ReadName( glob ) )
103 {
104 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d\n"
105 "%s" ),
106 __FILE__, __FUNCTION__, __LINE__ , proc.GetError() );
107
108 return false;
109 }
110
111 // expecting 'size'
112 if( !glob.compare( "size" ) )
113 {
114 if( !proc.ReadSFVec3f( size ) )
115 {
116 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d"
117 " * [INFO] invalid size %s\n"
118 " * [INFO] file: '%s'\n"
119 "%s" ),
120 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
121 proc.GetFileName(), proc.GetError() );
122
123 return false;
124 }
125
126 // for legacy KiCad support we interpret units as 0.1 inch
127 size *= 2.54;
128 }
129 else
130 {
131 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d"
132 " * [INFO] invalid Box %s\n"
133 " * [INFO] file: '%s'\n" ),
134 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
135 proc.GetFileName() );
136
137 return false;
138 }
139
140 if( size.x < 1e-6 || size.y < 1e-6 || size.z < 1e-6 )
141 {
142 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d"
143 " * [INFO] invalid Box size %s\n"
144 " * [INFO] file: '%s'\n" ),
145 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(),
146 proc.GetFileName() );
147
149 }
150
151 if( proc.Peek() == '}' )
152 {
153 proc.Pop();
154 return true;
155 }
156
157 wxLogTrace( traceVrmlPlugin, wxT( "%s:%s:%d"
158 " * [INFO] invalid size %s (no closing brace).\n"
159 " * [INFO] file: '%s'\n" ),
160 __FILE__, __FUNCTION__, __LINE__, proc.GetFilePosition(), proc.GetFileName() );
161
162 return false;
163}
164
165
167{
168 // this node may not own or reference any other node
169 wxCHECK_MSG( false, false, wxT( "AddRefNode is not applicable." ) );
170}
171
172
174{
175 // this node may not own or reference any other node
176 wxCHECK_MSG( false, false, wxT( "AddChildNode is not applicable." ) );
177}
178
179
181{
182 S3D::SGTYPES ptype = S3D::GetSGNodeType( aParent );
183
184 wxCHECK_MSG( aParent && ( ptype == S3D::SGTYPE_SHAPE ), nullptr,
185 wxString::Format( wxT( "Box does not have a Shape parent (parent ID: %s)" ),
186 ptype ) );
187
188 // do not render a bad box
189 if( size.x < 1e-6 || size.y < 1e-6 || size.z < 1e-6 )
190 return nullptr;
191
192 if( m_sgNode )
193 {
194 if( nullptr != aParent )
195 {
196 if( nullptr == S3D::GetSGNodeParent( m_sgNode )
197 && !S3D::AddSGNodeChild( aParent, m_sgNode ) )
198 {
199 return nullptr;
200 }
201 else if( aParent != S3D::GetSGNodeParent( m_sgNode )
202 && !S3D::AddSGNodeRef( aParent, m_sgNode ) )
203 {
204 return nullptr;
205 }
206 }
207
208 return m_sgNode;
209 }
210
211 // create the vertices, triangle indices, and normals
212 float x = size.x / 2.0;
213 float y = size.y / 2.0;
214 float z = size.z / 2.0;
215 std::vector< SGPOINT > vertices;
216 std::vector< SGVECTOR > norms;
217 std::vector< int > idx;
218 int base = 0;
219
220 vertices.reserve( 6 * 4 );
221 norms.reserve( 6 * 4 );
222 idx.reserve( 6 * 6 );
223
224 // top
225 vertices.emplace_back( -x, -y, z );
226 vertices.emplace_back( x, -y, z );
227 vertices.emplace_back( x, y, z );
228 vertices.emplace_back( -x, y, z );
229 norms.emplace_back( 0.0, 0.0, 1.0 );
230 norms.emplace_back( 0.0, 0.0, 1.0 );
231 norms.emplace_back( 0.0, 0.0, 1.0 );
232 norms.emplace_back( 0.0, 0.0, 1.0 );
233 idx.push_back( base );
234 idx.push_back( base + 1 );
235 idx.push_back( base + 2 );
236 idx.push_back( base );
237 idx.push_back( base + 2 );
238 idx.push_back( base + 3 );
239 base += 4;
240
241 // bottom
242 vertices.emplace_back( -x, -y, -z );
243 vertices.emplace_back( x, -y, -z );
244 vertices.emplace_back( x, y, -z );
245 vertices.emplace_back( -x, y, -z );
246 norms.emplace_back( 0.0, 0.0, -1.0 );
247 norms.emplace_back( 0.0, 0.0, -1.0 );
248 norms.emplace_back( 0.0, 0.0, -1.0 );
249 norms.emplace_back( 0.0, 0.0, -1.0 );
250 idx.push_back( base );
251 idx.push_back( base + 2 );
252 idx.push_back( base + 1 );
253 idx.push_back( base );
254 idx.push_back( base + 3 );
255 idx.push_back( base + 2 );
256 base += 4;
257
258 // front
259 vertices.emplace_back( -x, -y, z );
260 vertices.emplace_back( -x, -y, -z );
261 vertices.emplace_back( x, -y, -z );
262 vertices.emplace_back( x, -y, z );
263 norms.emplace_back( 0.0, -1.0, 0.0 );
264 norms.emplace_back( 0.0, -1.0, 0.0 );
265 norms.emplace_back( 0.0, -1.0, 0.0 );
266 norms.emplace_back( 0.0, -1.0, 0.0 );
267 idx.push_back( base );
268 idx.push_back( base + 1 );
269 idx.push_back( base + 2 );
270 idx.push_back( base );
271 idx.push_back( base + 2 );
272 idx.push_back( base + 3 );
273 base += 4;
274
275 // back
276 vertices.emplace_back( -x, y, z );
277 vertices.emplace_back( -x, y, -z );
278 vertices.emplace_back( x, y, -z );
279 vertices.emplace_back( x, y, z );
280 norms.emplace_back( 0.0, 1.0, 0.0 );
281 norms.emplace_back( 0.0, 1.0, 0.0 );
282 norms.emplace_back( 0.0, 1.0, 0.0 );
283 norms.emplace_back( 0.0, 1.0, 0.0 );
284 idx.push_back( base );
285 idx.push_back( base + 2 );
286 idx.push_back( base + 1 );
287 idx.push_back( base );
288 idx.push_back( base + 3 );
289 idx.push_back( base + 2 );
290 base += 4;
291
292 // left
293 vertices.emplace_back( -x, -y, -z );
294 vertices.emplace_back( -x, -y, z );
295 vertices.emplace_back( -x, y, z );
296 vertices.emplace_back( -x, y, -z );
297 norms.emplace_back( -1.0, 0.0, 0.0 );
298 norms.emplace_back( -1.0, 0.0, 0.0 );
299 norms.emplace_back( -1.0, 0.0, 0.0 );
300 norms.emplace_back( -1.0, 0.0, 0.0 );
301 idx.push_back( base );
302 idx.push_back( base + 1 );
303 idx.push_back( base + 2 );
304 idx.push_back( base );
305 idx.push_back( base + 2 );
306 idx.push_back( base + 3 );
307 base += 4;
308
309 // right
310 vertices.emplace_back( x, -y, -z );
311 vertices.emplace_back( x, -y, z );
312 vertices.emplace_back( x, y, z );
313 vertices.emplace_back( x, y, -z );
314 norms.emplace_back( 1.0, 0.0, 0.0 );
315 norms.emplace_back( 1.0, 0.0, 0.0 );
316 norms.emplace_back( 1.0, 0.0, 0.0 );
317 norms.emplace_back( 1.0, 0.0, 0.0 );
318 idx.push_back( base );
319 idx.push_back( base + 2 );
320 idx.push_back( base + 1 );
321 idx.push_back( base );
322 idx.push_back( base + 3 );
323 idx.push_back( base + 2 );
324
325 IFSG_FACESET fsNode( aParent );
326 IFSG_COORDS cpNode( fsNode );
327 cpNode.SetCoordsList( vertices.size(), &vertices[0] );
328 IFSG_COORDINDEX ciNode( fsNode );
329 ciNode.SetIndices( idx.size(), &idx[0] );
330 IFSG_NORMALS nmNode( fsNode );
331 nmNode.SetNormalList( norms.size(), &norms[0] );
332
333 m_sgNode = fsNode.GetRawPtr();
334
335 return m_sgNode;
336}
The wrapper for SGCOORDINDEX.
The wrapper for SGCOORDS.
Definition ifsg_coords.h:36
bool SetCoordsList(size_t aListSize, const SGPOINT *aCoordsList)
The wrapper for the SGFACESET class.
bool SetIndices(size_t nIndices, int *aIndexList)
Set the number of indices and creates a copy of the given index data.
SGNODE * GetRawPtr(void) noexcept
Return the raw internal SGNODE pointer.
Definition ifsg_node.cpp:61
The wrapper for the SGNORMALS class.
bool SetNormalList(size_t aListSize, const SGVECTOR *aNormalList)
The base class of all Scene Graph nodes.
Definition sg_node.h:71
The top node of a VRML2 model.
Definition vrml2_base.h:56
bool AddRefNode(WRL2NODE *aNode) override
bool isDangling(void) override
Determine whether an object should be moved to a different parent during the VRML to SG* translation.
Definition vrml2_box.cpp:59
bool AddChildNode(WRL2NODE *aNode) override
bool Read(WRLPROC &proc, WRL2BASE *aTopNode) override
Definition vrml2_box.cpp:70
SGNODE * TranslateToSG(SGNODE *aParent) override
Produce a representation of the data using the intermediate scenegraph structures of the kicad_3dsg l...
virtual ~WRL2BOX()
Definition vrml2_box.cpp:53
WRLVEC3F size
Definition vrml2_box.h:49
SGNODE * m_sgNode
Definition vrml2_node.h:174
WRL2NODE * m_Parent
Definition vrml2_node.h:165
WRL2NODES m_Type
Definition vrml2_node.h:166
void Pop(void)
Definition wrlproc.cpp:2031
char Peek(void)
Definition wrlproc.cpp:2003
std::string GetFileName(void)
Definition wrlproc.cpp:1991
std::string GetError(void)
Definition wrlproc.cpp:1956
bool eof(void)
Definition wrlproc.cpp:1950
bool ReadName(std::string &aName)
Definition wrlproc.cpp:285
std::string GetFilePosition() const
Definition wrlproc.cpp:1978
bool ReadSFVec3f(WRLVEC3F &aSFVec3f)
Definition wrlproc.cpp:1078
const wxChar *const traceVrmlPlugin
Flag to enable VRML plugin trace output.
Definition vrml.cpp:59
collects header files for all SG* wrappers and the API
SGLIB_API S3D::SGTYPES GetSGNodeType(SGNODE *aNode)
Definition ifsg_api.cpp:481
SGLIB_API SGNODE * GetSGNodeParent(SGNODE *aNode)
Definition ifsg_api.cpp:490
SGTYPES
Definition sg_types.h:32
@ SGTYPE_SHAPE
Definition sg_types.h:41
SGLIB_API bool AddSGNodeChild(SGNODE *aParent, SGNODE *aChild)
Definition ifsg_api.cpp:508
SGLIB_API bool AddSGNodeRef(SGNODE *aParent, SGNODE *aChild)
Definition ifsg_api.cpp:499