KiCad PCB EDA Suite
Loading...
Searching...
No Matches
writer.h
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) 2025 Mark Roszko <[email protected]>
5 * Copyright (C) 2010 - 2024 Fiji developers (Java source used as reference)
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#pragma once
27
28#include <cstdint>
29#include <memory>
30#include <string>
31#include <unordered_map>
32
35
36#include <TopoDS_Shape.hxx>
37#include <TDocStd_Document.hxx>
38#include <Standard_Handle.hxx>
39#include <Graphic3d_Vec4.hxx>
40#include <Graphic3d_Vec3.hxx>
41#include <Bnd_Box.hxx>
42#include <XCAFDoc_ShapeTool.hxx>
43#include <XCAFDoc_ColorTool.hxx>
44#include <XCAFDoc_VisMaterialTool.hxx>
45
46#include <math/vector2d.h>
47#include <math/vector3.h>
48
49#include <wx/mstream.h>
50
51namespace std
52{
53template <>
54struct hash<Graphic3d_Vec4>
55{
56 size_t operator()( const Graphic3d_Vec4& v ) const
57 {
58 size_t h1 = std::hash<float>{}( v.x() );
59 size_t h2 = std::hash<float>{}( v.y() );
60 size_t h3 = std::hash<float>{}( v.z() );
61 size_t h4 = std::hash<float>{}( v.w() );
62 return h1 ^ ( h2 << 1 ) ^ ( h3 << 2 ) ^ ( h4 << 3 );
63 }
64};
65} // namespace std
66
67namespace U3D
68{
69class MESH
70{
71public:
72 std::vector<VECTOR3D> coords;
73
74 std::vector<VECTOR3D> normals;
75
79 std::vector<Graphic3d_Vec4> diffuse_colors;
80
84 std::vector<Graphic3d_Vec4> specular_colors;
88 std::unordered_map<Graphic3d_Vec4, uint32_t> diffuse_colors_index_map;
92 std::unordered_map<Graphic3d_Vec4, uint32_t> specular_colors_index_map;
93
97 std::vector<uint32_t> coordIndices;
98
102 std::vector<uint32_t> normalIndices;
103
107 std::vector<uint32_t> diffuseColorIndices;
108
112 std::vector<uint32_t> specularColorIndices;
113
117 Graphic3d_Vec4 diffuse_color;
118
122 Graphic3d_Vec3 specular_color;
123
127 std::string name;
128
132 std::string parentName;
133
135
136 bool IsEmpty()
137 {
138 return coords.empty() || coordIndices.empty();
139 }
140
141 uint32_t GetOrAddUniqueDiffuseColor( const Graphic3d_Vec4& aColor )
142 {
143 uint32_t colorIndex = 0;
144 if( diffuse_colors_index_map.find( aColor ) != diffuse_colors_index_map.end() )
145 {
146 colorIndex = diffuse_colors_index_map[aColor];
147 }
148 else
149 {
150 colorIndex = diffuse_colors.size();
151 diffuse_colors_index_map[aColor] = colorIndex;
152 diffuse_colors.push_back( aColor );
153 }
154
155 return colorIndex;
156 }
157
158 uint32_t GetOrAddUniqueSpecularColor( const Graphic3d_Vec4& aColor )
159 {
160 uint32_t colorIndex = 0;
161 if( specular_colors_index_map.find( aColor ) != specular_colors_index_map.end() )
162 {
163 colorIndex = specular_colors_index_map[aColor];
164 }
165 else
166 {
167 colorIndex = specular_colors.size();
168 specular_colors_index_map[aColor] = colorIndex;
169 specular_colors.push_back( aColor );
170 }
171
172 return colorIndex;
173 }
174};
175
177{
181 std::string name;
182
186 std::vector<float> mat;
187};
188
190{
194 std::string name;
195
199 std::vector<PARENT_NODE> parentNodes;
200};
201
203{
204public:
205 WRITER( const std::string& aFilename );
206
207 bool Perform( const Handle( TDocStd_Document ) & aDocument );
208
209 const VECTOR3D& GetCenter() const { return m_center; }
210 const Bnd_Box& GetMeshBoundingBox() const { return m_meshBoundingBox; }
211private:
212 void writeMatrix( BIT_STREAM_WRITER& aBitStreamWriter, const std::vector<float>& aMat );
213
214 std::shared_ptr<DATA_BLOCK> getGroupNodeBlock( const std::string& aGroupNodeName,
215 const PARENT_NODE* aParentNode );
216
217 std::shared_ptr<DATA_BLOCK> getModelNodeBlock( const std::string& aModelNodeName,
218 const std::string& aParentNodeName,
219 const std::string& aModelResourceName,
220 const std::vector<float>& aMat );
221
222 std::shared_ptr<DATA_BLOCK> getShadingModifierBlock( const std::string& aShadingModName,
223 const std::string& aShaderName );
224
225 std::shared_ptr<DATA_BLOCK>
226 getNodeModifierChain( const std::string& aModifierChainName, const std::string& aModelNodeName,
227 const std::string& aParentNodeName, const std::string& aModelResourceName,
228 const std::string& aShaderName, const std::vector<float>& aMat );
229
230
231 std::shared_ptr<DATA_BLOCK> getHeaderBlock( uint32_t aDeclSize, uint32_t aContSize );
232
233
234 std::shared_ptr<DATA_BLOCK> getLitTextureShaderBlock( const std::string& aShaderName,
235 const std::string& aMaterialName );
236
237
238 std::shared_ptr<DATA_BLOCK> getMaterialResourceBlock( const std::string& aMaterialName,
239 const Graphic3d_Vec4& aDiffuseColor,
240 const Graphic3d_Vec3& aSpecularColor );
241
242 std::shared_ptr<DATA_BLOCK>
243 getModelResourceModifierChain( const std::string& aModifierChainName, const MESH* aMesh,
244 const std::string& aMeshname );
245
246 std::shared_ptr<DATA_BLOCK> getMeshDeclarationBlock( const MESH* aMesh,
247 const std::string& aMeshName );
248
249 std::shared_ptr<DATA_BLOCK> getMeshContinuationBlock( const MESH* aMesh,
250 const std::string& aMeshName );
251
252
253 std::shared_ptr<DATA_BLOCK> getLightModifierChain( const std::string& aModifierChainName,
254 const std::string& aLightResourceName );
255
256 std::shared_ptr<DATA_BLOCK> getLightNodeBlock( const std::string& aLightNodeName,
257 const std::string& aLightResourceName );
258
259 std::shared_ptr<DATA_BLOCK> getLightResourceBlock( const std::string& aLightResourceName );
260
261 std::shared_ptr<DATA_BLOCK>
262 getGroupNodeModifierChain( const std::string& aModifierChainName,
263 const std::vector<GROUP_NODE>& aGroupNodes );
264
265 uint32_t writeDataBlock( std::shared_ptr<DATA_BLOCK> b, wxMemoryOutputStream& aStream );
266
267
268 void generateMeshesByAssembly( const Handle( TDocStd_Document ) & doc );
269
270 void collectGeometryRecursive( const TDF_Label& label, const Handle( XCAFDoc_ShapeTool ) & shapeTool,
271 const Handle( XCAFDoc_ColorTool ) & colorTool,
272 const Handle( XCAFDoc_VisMaterialTool ) & visMatTool,
273 const gp_Trsf& cumulativeTransform,
274 const std::string& baseName,
275 std::unordered_map<Graphic3d_Vec4, MESH*>& meshesByColor );
276
277
278 void getMeshName( const TDF_Label& label, Handle( XCAFDoc_ShapeTool ) shapeTool, MESH* mesh );
279
280
281 std::string m_filename;
284 std::map<std::string, int> m_meshDedupMap;
285 const uint32_t m_contextBaseShadingID = 1;
287 std::vector<std::unique_ptr<MESH>> m_meshes;
288 std::vector<GROUP_NODE> m_groupNodes; // dynamic + root grouping nodes
289};
290
291} // namespace U3D
Implements the bit stream writer functionality.
bool IsEmpty()
Definition writer.h:136
Graphic3d_Vec3 specular_color
Specular color used if not using per-vertex color.
Definition writer.h:122
bool perVertexColor
Definition writer.h:134
std::vector< uint32_t > normalIndices
Coordinate indices, maps vertex normal positions to triangles.
Definition writer.h:102
std::vector< uint32_t > specularColorIndices
Coordinate indices, maps each vertex to a specular color.
Definition writer.h:112
std::vector< VECTOR3D > normals
Definition writer.h:74
std::vector< Graphic3d_Vec4 > diffuse_colors
List of all unique diffuse colors.
Definition writer.h:79
std::unordered_map< Graphic3d_Vec4, uint32_t > specular_colors_index_map
Index map helps select the color index for a given vertex when creating the colorIndices.
Definition writer.h:92
uint32_t GetOrAddUniqueSpecularColor(const Graphic3d_Vec4 &aColor)
Definition writer.h:158
std::vector< uint32_t > coordIndices
Coordinate indices, maps vertex positions to triangles.
Definition writer.h:97
std::unordered_map< Graphic3d_Vec4, uint32_t > diffuse_colors_index_map
Index map helps select the color index for a given vertex when creating the colorIndices.
Definition writer.h:88
std::vector< uint32_t > diffuseColorIndices
Coordinate indices, maps each vertex to a diffuse color.
Definition writer.h:107
std::string name
The name of the mesh, this will be visible in U3D viewers.
Definition writer.h:127
std::vector< VECTOR3D > coords
Definition writer.h:72
uint32_t GetOrAddUniqueDiffuseColor(const Graphic3d_Vec4 &aColor)
Definition writer.h:141
Graphic3d_Vec4 diffuse_color
Diffuse color used if not using per-vertex color.
Definition writer.h:117
std::vector< Graphic3d_Vec4 > specular_colors
List of all unique specular colors.
Definition writer.h:84
std::string parentName
Parent name (if any) to group the mesh under.
Definition writer.h:132
std::string m_filename
Definition writer.h:281
std::vector< GROUP_NODE > m_groupNodes
Definition writer.h:288
std::shared_ptr< DATA_BLOCK > getHeaderBlock(uint32_t aDeclSize, uint32_t aContSize)
Definition writer.cpp:674
std::shared_ptr< DATA_BLOCK > getShadingModifierBlock(const std::string &aShadingModName, const std::string &aShaderName)
Definition writer.cpp:587
std::shared_ptr< DATA_BLOCK > getLitTextureShaderBlock(const std::string &aShaderName, const std::string &aMaterialName)
Definition writer.cpp:694
void writeMatrix(BIT_STREAM_WRITER &aBitStreamWriter, const std::vector< float > &aMat)
Definition writer.cpp:534
bool Perform(const Handle(TDocStd_Document) &aDocument)
Definition writer.cpp:967
std::shared_ptr< DATA_BLOCK > getModelNodeBlock(const std::string &aModelNodeName, const std::string &aParentNodeName, const std::string &aModelResourceName, const std::vector< float > &aMat)
Definition writer.cpp:562
void getMeshName(const TDF_Label &label, Handle(XCAFDoc_ShapeTool) shapeTool, MESH *mesh)
Definition writer.cpp:249
WRITER(const std::string &aFilename)
Definition writer.cpp:524
const uint32_t m_contextBaseShadingID
Definition writer.h:285
std::shared_ptr< DATA_BLOCK > getMeshContinuationBlock(const MESH *aMesh, const std::string &aMeshName)
Definition writer.cpp:811
void generateMeshesByAssembly(const Handle(TDocStd_Document) &doc)
Definition writer.cpp:434
std::shared_ptr< DATA_BLOCK > getLightNodeBlock(const std::string &aLightNodeName, const std::string &aLightResourceName)
Definition writer.cpp:901
std::shared_ptr< DATA_BLOCK > getModelResourceModifierChain(const std::string &aModifierChainName, const MESH *aMesh, const std::string &aMeshname)
Definition writer.cpp:606
std::shared_ptr< DATA_BLOCK > getMaterialResourceBlock(const std::string &aMaterialName, const Graphic3d_Vec4 &aDiffuseColor, const Graphic3d_Vec3 &aSpecularColor)
Definition writer.cpp:718
const VECTOR3D & GetCenter() const
Definition writer.h:209
std::shared_ptr< DATA_BLOCK > getGroupNodeModifierChain(const std::string &aModifierChainName, const std::vector< GROUP_NODE > &aGroupNodes)
Definition writer.cpp:627
std::vector< std::unique_ptr< MESH > > m_meshes
Definition writer.h:287
std::shared_ptr< DATA_BLOCK > getLightModifierChain(const std::string &aModifierChainName, const std::string &aLightResourceName)
Definition writer.cpp:882
void collectGeometryRecursive(const TDF_Label &label, const Handle(XCAFDoc_ShapeTool) &shapeTool, const Handle(XCAFDoc_ColorTool) &colorTool, const Handle(XCAFDoc_VisMaterialTool) &visMatTool, const gp_Trsf &cumulativeTransform, const std::string &baseName, std::unordered_map< Graphic3d_Vec4, MESH * > &meshesByColor)
Definition writer.cpp:289
std::shared_ptr< DATA_BLOCK > getLightResourceBlock(const std::string &aLightResourceName)
Definition writer.cpp:918
bool m_includeNormals
Definition writer.h:286
uint32_t writeDataBlock(std::shared_ptr< DATA_BLOCK > b, wxMemoryOutputStream &aStream)
Definition writer.cpp:940
std::map< std::string, int > m_meshDedupMap
Definition writer.h:284
std::shared_ptr< DATA_BLOCK > getNodeModifierChain(const std::string &aModifierChainName, const std::string &aModelNodeName, const std::string &aParentNodeName, const std::string &aModelResourceName, const std::string &aShaderName, const std::vector< float > &aMat)
Definition writer.cpp:650
std::shared_ptr< DATA_BLOCK > getGroupNodeBlock(const std::string &aGroupNodeName, const PARENT_NODE *aParentNode)
Definition writer.cpp:541
const Bnd_Box & GetMeshBoundingBox() const
Definition writer.h:210
Bnd_Box m_meshBoundingBox
Definition writer.h:283
VECTOR3D m_center
Definition writer.h:282
std::shared_ptr< DATA_BLOCK > getMeshDeclarationBlock(const MESH *aMesh, const std::string &aMeshName)
Definition writer.cpp:751
Handle(KICAD3D_INFO) KICAD3D_INFO
STL namespace.
std::vector< PARENT_NODE > parentNodes
List of parent nodes this group node belongs to.
Definition writer.h:199
std::string name
Name of this group node.
Definition writer.h:194
std::vector< float > mat
Transform matrix, 4x4, row major.
Definition writer.h:186
std::string name
Name of the parent node to link to.
Definition writer.h:181
size_t operator()(const Graphic3d_Vec4 &v) const
Definition writer.h:56
VECTOR3< double > VECTOR3D
Definition vector3.h:230