VTK  9.7.1
vtkOBJImporterInternals.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
3 #ifndef vtkOBJImporterInternals_h
4 #define vtkOBJImporterInternals_h
5 
6 #include "vtkIOImportModule.h" // For export macro
7 #include "vtkPolyDataAlgorithm.h"
8 #include <map> // for std::map
9 #include <string> // for std::string
10 #include <vector> // for std::vector
11 
12 VTK_ABI_NAMESPACE_BEGIN
13 class vtkActor;
14 class vtkActorCollection;
15 class vtkResourceStream;
16 
17 struct VTKIOIMPORT_EXPORT vtkOBJImportedMaterial
18 {
21  double amb[3];
22  double diff[3];
23  double spec[3];
24  double map_Kd_scale[3];
25  double map_Kd_offset[3];
26  int illum;
27  double reflect;
28  double refract;
29  double trans;
30  double specularPower;
31  double glossy;
32  double refract_index;
33  const char* GetClassName() { return "vtkOBJImportedMaterial"; }
35 };
36 
37 VTKIOIMPORT_EXPORT
39 
40 struct vtkOBJImportedPolyDataWithMaterial;
41 
42 class VTKIOIMPORT_EXPORT vtkOBJPolyDataProcessor : public vtkPolyDataAlgorithm
43 {
44 public:
45  static vtkOBJPolyDataProcessor* New();
47  void PrintSelf(ostream& os, vtkIndent indent) override;
48 
49  // Description:
50  // Specify filenames / streams to read of Wavefront .obj file.
51  void SetStream(vtkResourceStream* stream) { this->Stream = stream; }
52  void SetFileName(const char* arg)
53  {
54  if (arg == nullptr)
55  {
56  return;
57  }
58  if (this->FileName == arg)
59  {
60  return;
61  }
62  this->FileName = std::string(arg);
63  }
64  void SetMTLStream(vtkResourceStream* mtlStream) { this->MTLStream = mtlStream; }
65  void SetMTLfileName(const char* arg)
66  {
67  if (arg == nullptr)
68  {
69  return;
70  }
71  if (this->MTLFileName == arg)
72  {
73  return;
74  }
75  this->MTLFileName = std::string(arg);
76  this->DefaultMTLFileName = false;
77  }
78  void SetTextureStreams(std::map<std::string, vtkResourceStream*> streamMap)
79  {
80  this->TextureStreams = streamMap;
81  }
82  void SetTexturePath(const char* arg)
83  {
84  this->TexturePath = std::string(arg);
85  if (this->TexturePath.empty())
86  {
87  return;
88  }
89 #if defined(_WIN32)
90  const char sep = '\\';
91 #else
92  const char sep = '/';
93 #endif
94  if (this->TexturePath.at(this->TexturePath.size() - 1) != sep)
95  {
96  this->TexturePath += sep;
97  }
98  this->DefaultTexturePath = false;
99  }
100  const std::string& GetTexturePath() const { return this->TexturePath; }
101  const std::map<std::string, vtkResourceStream*>& GetTextureStreams() const
102  {
103  return this->TextureStreams;
104  }
105 
106  const std::string& GetFileName() const { return this->FileName; }
107 
108  const std::string& GetMTLFileName() const { return this->MTLFileName; }
109 
110  vtkSetMacro(VertexScale, double);
111  vtkGetMacro(VertexScale, double);
112  vtkGetMacro(SuccessParsingFiles, int);
113 
114  virtual vtkPolyData* GetOutput(int idx);
115 
116  int GetNumberOfOutputs();
117 
118  vtkOBJImportedMaterial* GetMaterial(int k);
119 
120  std::string GetTextureFilename(int idx); // return string by index
121 
122  double VertexScale; // scale vertices by this during import
123 
124  std::vector<vtkOBJImportedMaterial*> parsedMTLs;
125  std::map<std::string, vtkOBJImportedMaterial*> mtlName_to_mtlData;
126 
127  // our internal parsing/storage
128  std::vector<vtkOBJImportedPolyDataWithMaterial*> poly_list;
129 
130  // what gets returned to client code via GetOutput()
131  std::vector<vtkSmartPointer<vtkPolyData>> outVector_of_vtkPolyData;
132 
133  std::vector<vtkSmartPointer<vtkActor>> actor_list;
135 
136  std::vector<vtkOBJImportedMaterial*> ParseOBJandMTL(
137  vtkResourceStream* mtlStream, int& result_code);
138 
139  void ReadVertices(bool gotFirstUseMaterialTag, char* pLine, float xyz, int lineNr, double v_scale,
140  bool everything_ok, vtkPoints* points, bool use_scale);
141 
142 protected:
144  ~vtkOBJPolyDataProcessor() override;
146  /*override*/;
147 
148  vtkSetMacro(SuccessParsingFiles, int);
149 
150  vtkResourceStream* Stream = nullptr; // Stream of obj being read
151  std::string FileName; // filename (.obj) being read
152 
153  bool DefaultMTLFileName; // tells whether default MTL should be used
154  std::string MTLFileName; // associated .mtl to *.obj, typically it is *.obj.mtl
155  vtkResourceStream* MTLStream = nullptr; // Stream of associated MTL file
156 
157  bool DefaultTexturePath; // tells whether default texture path should be used
158  std::string TexturePath; // Texture path to read
159  std::map<std::string, vtkResourceStream*> TextureStreams;
160 
161  int SuccessParsingFiles;
162 
163 private:
165  void operator=(const vtkOBJPolyDataProcessor&) = delete;
166 };
167 
168 class vtkRenderWindow;
169 class vtkRenderer;
170 VTKIOIMPORT_EXPORT
171 bool bindTexturedPolydataToRenderWindow(vtkRenderWindow* renderWindow, vtkRenderer* renderer,
172  vtkOBJPolyDataProcessor* reader, vtkActorCollection* actorCollection);
173 
174 VTK_ABI_NAMESPACE_END
175 #endif
void SetMTLfileName(const char *arg)
represents an object (geometry & properties) in a rendered scene
Definition: vtkActor.h:41
Abstract class used for custom streams.
void SetMTLStream(vtkResourceStream *mtlStream)
Store vtkAlgorithm input/output information.
void SetStream(vtkResourceStream *stream)
virtual int RequestData(vtkInformation *request, vtkInformationVector **inputVector, vtkInformationVector *outputVector)
This is called by the superclass.
const std::map< std::string, vtkResourceStream * > & GetTextureStreams() const
std::vector< vtkOBJImportedPolyDataWithMaterial * > poly_list
abstract specification for renderers
Definition: vtkRenderer.h:63
concrete dataset represents vertices, lines, polygons, and triangle strips
Definition: vtkPolyData.h:71
const std::string & GetMTLFileName() const
const std::string & GetFileName() const
static vtkPolyDataAlgorithm * New()
Superclass for algorithms that produce only polydata as output.
void SetFileName(const char *arg)
a simple class to control print indentation
Definition: vtkIndent.h:28
std::map< std::string, vtkOBJImportedMaterial * > mtlName_to_mtlData
VTKIOIMPORT_EXPORT void obj_set_material_defaults(vtkOBJImportedMaterial *mtl)
std::vector< vtkOBJImportedMaterial * > parsedMTLs
an ordered list of actors
std::vector< vtkSmartPointer< vtkActor > > actor_list
void SetTextureStreams(std::map< std::string, vtkResourceStream * > streamMap)
create a window for renderers to draw into
const std::string & GetTexturePath() const
void SetTexturePath(const char *arg)
Store zero or more vtkInformation instances.
std::vector< vtkSmartPointer< vtkPolyData > > outVector_of_vtkPolyData
vtkPolyData * GetOutput()
Get the output data object for a port on this algorithm.
represent and manipulate 3D points
Definition: vtkPoints.h:30
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
VTKIOIMPORT_EXPORT bool bindTexturedPolydataToRenderWindow(vtkRenderWindow *renderWindow, vtkRenderer *renderer, vtkOBJPolyDataProcessor *reader, vtkActorCollection *actorCollection)