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, see <https://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24#include <cstdint>
25#include <memory>
26#include <string>
27#include <unordered_map>
28
31
32#include <TopoDS_Shape.hxx>
33#include <TDocStd_Document.hxx>
34#include <Standard_Handle.hxx>
35#include <NCollection_Vec4.hxx>
36#include <NCollection_Vec3.hxx>
37#include <Bnd_Box.hxx>
38#include <XCAFDoc_ShapeTool.hxx>
39#include <XCAFDoc_ColorTool.hxx>
40#include <XCAFDoc_VisMaterialTool.hxx>
41
42#include <math/vector2d.h>
43#include <math/vector3.h>
44
45#include <wx/mstream.h>
46
47namespace std
48{
49template <>
50struct hash<NCollection_Vec4<float>>
51{
52 size_t operator()( const NCollection_Vec4<float>& v ) const
53 {
54 size_t h1 = std::hash<float>{}( v.x() );
55 size_t h2 = std::hash<float>{}( v.y() );
56 size_t h3 = std::hash<float>{}( v.z() );
57 size_t h4 = std::hash<float>{}( v.w() );
58 return h1 ^ ( h2 << 1 ) ^ ( h3 << 2 ) ^ ( h4 << 3 );
59 }
60};
61} // namespace std
62
63namespace U3D
64{
65class MESH
66{
67public:
68 std::vector<VECTOR3D> coords;
69
70 std::vector<VECTOR3D> normals;
71
75 std::vector<NCollection_Vec4<float>> diffuse_colors;
76
80 std::vector<NCollection_Vec4<float>> specular_colors;
84 std::unordered_map<NCollection_Vec4<float>, uint32_t> diffuse_colors_index_map;
88 std::unordered_map<NCollection_Vec4<float>, uint32_t> specular_colors_index_map;
89
93 std::vector<uint32_t> coordIndices;
94
98 std::vector<uint32_t> normalIndices;
99
103 std::vector<uint32_t> diffuseColorIndices;
104
108 std::vector<uint32_t> specularColorIndices;
109
113 NCollection_Vec4<float> diffuse_color;
114
118 NCollection_Vec3<float> specular_color;
119
123 std::string name;
124
128 std::string parentName;
129
131
132 bool IsEmpty()
133 {
134 return coords.empty() || coordIndices.empty();
135 }
136
137 uint32_t GetOrAddUniqueDiffuseColor( const NCollection_Vec4<float>& aColor )
138 {
139 uint32_t colorIndex = 0;
140 if( diffuse_colors_index_map.find( aColor ) != diffuse_colors_index_map.end() )
141 {
142 colorIndex = diffuse_colors_index_map[aColor];
143 }
144 else
145 {
146 colorIndex = diffuse_colors.size();
147 diffuse_colors_index_map[aColor] = colorIndex;
148 diffuse_colors.push_back( aColor );
149 }
150
151 return colorIndex;
152 }
153
154 uint32_t GetOrAddUniqueSpecularColor( const NCollection_Vec4<float>& aColor )
155 {
156 uint32_t colorIndex = 0;
157 if( specular_colors_index_map.find( aColor ) != specular_colors_index_map.end() )
158 {
159 colorIndex = specular_colors_index_map[aColor];
160 }
161 else
162 {
163 colorIndex = specular_colors.size();
164 specular_colors_index_map[aColor] = colorIndex;
165 specular_colors.push_back( aColor );
166 }
167
168 return colorIndex;
169 }
170};
171
173{
177 std::string name;
178
182 std::vector<float> mat;
183};
184
186{
190 std::string name;
191
195 std::vector<PARENT_NODE> parentNodes;
196};
197
199{
200public:
201 WRITER( const std::string& aFilename );
202
203 bool Perform( const Handle( TDocStd_Document ) & aDocument );
204
205 const VECTOR3D& GetCenter() const { return m_center; }
206 const Bnd_Box& GetMeshBoundingBox() const { return m_meshBoundingBox; }
207private:
208 void writeMatrix( BIT_STREAM_WRITER& aBitStreamWriter, const std::vector<float>& aMat );
209
210 std::shared_ptr<DATA_BLOCK> getGroupNodeBlock( const std::string& aGroupNodeName,
211 const PARENT_NODE* aParentNode );
212
213 std::shared_ptr<DATA_BLOCK> getModelNodeBlock( const std::string& aModelNodeName,
214 const std::string& aParentNodeName,
215 const std::string& aModelResourceName,
216 const std::vector<float>& aMat );
217
218 std::shared_ptr<DATA_BLOCK> getShadingModifierBlock( const std::string& aShadingModName,
219 const std::string& aShaderName );
220
221 std::shared_ptr<DATA_BLOCK>
222 getNodeModifierChain( const std::string& aModifierChainName, const std::string& aModelNodeName,
223 const std::string& aParentNodeName, const std::string& aModelResourceName,
224 const std::string& aShaderName, const std::vector<float>& aMat );
225
226
227 std::shared_ptr<DATA_BLOCK> getHeaderBlock( uint32_t aDeclSize, uint32_t aContSize );
228
229
230 std::shared_ptr<DATA_BLOCK> getLitTextureShaderBlock( const std::string& aShaderName,
231 const std::string& aMaterialName );
232
233
234 std::shared_ptr<DATA_BLOCK> getMaterialResourceBlock( const std::string& aMaterialName,
235 const NCollection_Vec4<float>& aDiffuseColor,
236 const NCollection_Vec3<float>& aSpecularColor );
237
238 std::shared_ptr<DATA_BLOCK>
239 getModelResourceModifierChain( const std::string& aModifierChainName, const MESH* aMesh,
240 const std::string& aMeshname );
241
242 std::shared_ptr<DATA_BLOCK> getMeshDeclarationBlock( const MESH* aMesh,
243 const std::string& aMeshName );
244
245 std::shared_ptr<DATA_BLOCK> getMeshContinuationBlock( const MESH* aMesh,
246 const std::string& aMeshName );
247
248
249 std::shared_ptr<DATA_BLOCK> getLightModifierChain( const std::string& aModifierChainName,
250 const std::string& aLightResourceName );
251
252 std::shared_ptr<DATA_BLOCK> getLightNodeBlock( const std::string& aLightNodeName,
253 const std::string& aLightResourceName );
254
255 std::shared_ptr<DATA_BLOCK> getLightResourceBlock( const std::string& aLightResourceName );
256
257 std::shared_ptr<DATA_BLOCK>
258 getGroupNodeModifierChain( const std::string& aModifierChainName,
259 const std::vector<GROUP_NODE>& aGroupNodes );
260
261 uint32_t writeDataBlock( std::shared_ptr<DATA_BLOCK> b, wxMemoryOutputStream& aStream );
262
263
264 void generateMeshesByAssembly( const Handle( TDocStd_Document ) & doc );
265
266 void collectGeometryRecursive( const TDF_Label& label, const Handle( XCAFDoc_ShapeTool ) & shapeTool,
267 const Handle( XCAFDoc_ColorTool ) & colorTool,
268 const Handle( XCAFDoc_VisMaterialTool ) & visMatTool,
269 const gp_Trsf& cumulativeTransform,
270 const std::string& baseName,
271 std::unordered_map<NCollection_Vec4<float>, MESH*>& meshesByColor );
272
273
274 void getMeshName( const TDF_Label& label, Handle( XCAFDoc_ShapeTool ) shapeTool, MESH* mesh );
275
276
277 std::string m_filename;
280 std::map<std::string, int> m_meshDedupMap;
281 const uint32_t m_contextBaseShadingID = 1;
283 std::vector<std::unique_ptr<MESH>> m_meshes;
284 std::vector<GROUP_NODE> m_groupNodes; // dynamic + root grouping nodes
285};
286
287} // namespace U3D
Implements the bit stream writer functionality.
bool IsEmpty()
Definition writer.h:132
bool perVertexColor
Definition writer.h:130
std::vector< uint32_t > normalIndices
Coordinate indices, maps vertex normal positions to triangles.
Definition writer.h:98
std::vector< uint32_t > specularColorIndices
Coordinate indices, maps each vertex to a specular color.
Definition writer.h:108
std::vector< VECTOR3D > normals
Definition writer.h:70
NCollection_Vec3< float > specular_color
Specular color used if not using per-vertex color.
Definition writer.h:118
uint32_t GetOrAddUniqueDiffuseColor(const NCollection_Vec4< float > &aColor)
Definition writer.h:137
std::vector< NCollection_Vec4< float > > diffuse_colors
List of all unique diffuse colors.
Definition writer.h:75
std::vector< uint32_t > coordIndices
Coordinate indices, maps vertex positions to triangles.
Definition writer.h:93
uint32_t GetOrAddUniqueSpecularColor(const NCollection_Vec4< float > &aColor)
Definition writer.h:154
std::vector< NCollection_Vec4< float > > specular_colors
List of all unique specular colors.
Definition writer.h:80
NCollection_Vec4< float > diffuse_color
Diffuse color used if not using per-vertex color.
Definition writer.h:113
std::unordered_map< NCollection_Vec4< float >, uint32_t > diffuse_colors_index_map
Index map helps select the color index for a given vertex when creating the colorIndices.
Definition writer.h:84
std::vector< uint32_t > diffuseColorIndices
Coordinate indices, maps each vertex to a diffuse color.
Definition writer.h:103
std::unordered_map< NCollection_Vec4< float >, uint32_t > specular_colors_index_map
Index map helps select the color index for a given vertex when creating the colorIndices.
Definition writer.h:88
std::string name
The name of the mesh, this will be visible in U3D viewers.
Definition writer.h:123
std::vector< VECTOR3D > coords
Definition writer.h:68
std::string parentName
Parent name (if any) to group the mesh under.
Definition writer.h:128
std::string m_filename
Definition writer.h:277
std::vector< GROUP_NODE > m_groupNodes
Definition writer.h:284
std::shared_ptr< DATA_BLOCK > getHeaderBlock(uint32_t aDeclSize, uint32_t aContSize)
Definition writer.cpp:670
std::shared_ptr< DATA_BLOCK > getShadingModifierBlock(const std::string &aShadingModName, const std::string &aShaderName)
Definition writer.cpp:583
std::shared_ptr< DATA_BLOCK > getLitTextureShaderBlock(const std::string &aShaderName, const std::string &aMaterialName)
Definition writer.cpp:690
std::shared_ptr< DATA_BLOCK > getMaterialResourceBlock(const std::string &aMaterialName, const NCollection_Vec4< float > &aDiffuseColor, const NCollection_Vec3< float > &aSpecularColor)
Definition writer.cpp:714
void writeMatrix(BIT_STREAM_WRITER &aBitStreamWriter, const std::vector< float > &aMat)
Definition writer.cpp:530
bool Perform(const Handle(TDocStd_Document) &aDocument)
Definition writer.cpp:963
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:558
void getMeshName(const TDF_Label &label, Handle(XCAFDoc_ShapeTool) shapeTool, MESH *mesh)
Definition writer.cpp:245
WRITER(const std::string &aFilename)
Definition writer.cpp:520
const uint32_t m_contextBaseShadingID
Definition writer.h:281
std::shared_ptr< DATA_BLOCK > getMeshContinuationBlock(const MESH *aMesh, const std::string &aMeshName)
Definition writer.cpp:807
void generateMeshesByAssembly(const Handle(TDocStd_Document) &doc)
Definition writer.cpp:430
std::shared_ptr< DATA_BLOCK > getLightNodeBlock(const std::string &aLightNodeName, const std::string &aLightResourceName)
Definition writer.cpp:897
std::shared_ptr< DATA_BLOCK > getModelResourceModifierChain(const std::string &aModifierChainName, const MESH *aMesh, const std::string &aMeshname)
Definition writer.cpp:602
const VECTOR3D & GetCenter() const
Definition writer.h:205
std::shared_ptr< DATA_BLOCK > getGroupNodeModifierChain(const std::string &aModifierChainName, const std::vector< GROUP_NODE > &aGroupNodes)
Definition writer.cpp:623
std::vector< std::unique_ptr< MESH > > m_meshes
Definition writer.h:283
std::shared_ptr< DATA_BLOCK > getLightModifierChain(const std::string &aModifierChainName, const std::string &aLightResourceName)
Definition writer.cpp:878
std::shared_ptr< DATA_BLOCK > getLightResourceBlock(const std::string &aLightResourceName)
Definition writer.cpp:914
bool m_includeNormals
Definition writer.h:282
uint32_t writeDataBlock(std::shared_ptr< DATA_BLOCK > b, wxMemoryOutputStream &aStream)
Definition writer.cpp:936
std::map< std::string, int > m_meshDedupMap
Definition writer.h:280
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< NCollection_Vec4< float >, MESH * > &meshesByColor)
Definition writer.cpp:285
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:646
std::shared_ptr< DATA_BLOCK > getGroupNodeBlock(const std::string &aGroupNodeName, const PARENT_NODE *aParentNode)
Definition writer.cpp:537
const Bnd_Box & GetMeshBoundingBox() const
Definition writer.h:206
Bnd_Box m_meshBoundingBox
Definition writer.h:279
VECTOR3D m_center
Definition writer.h:278
std::shared_ptr< DATA_BLOCK > getMeshDeclarationBlock(const MESH *aMesh, const std::string &aMeshName)
Definition writer.cpp:747
Handle(KICAD3D_INFO) KICAD3D_INFO
STL namespace.
std::vector< PARENT_NODE > parentNodes
List of parent nodes this group node belongs to.
Definition writer.h:195
std::string name
Name of this group node.
Definition writer.h:190
std::vector< float > mat
Transform matrix, 4x4, row major.
Definition writer.h:182
std::string name
Name of the parent node to link to.
Definition writer.h:177
size_t operator()(const NCollection_Vec4< float > &v) const
Definition writer.h:52
VECTOR3< double > VECTOR3D
Definition vector3.h:230