Fermat
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
pbrt_parser.h
1 /*
2  * Fermat
3  *
4  * Copyright (c) 2016-2019, NVIDIA CORPORATION. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the NVIDIA CORPORATION nor the
14  * names of its contributors may be used to endorse or promote products
15  * derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #pragma once
30 
31 #include <buffers.h>
32 #include <vector>
33 #include <string>
34 #include <stdexcept>
35 
36 namespace pbrt {
37 
38 class PBRTParserError : public std::runtime_error
39 {
40 public:
41  explicit PBRTParserError( const std::string& what_arg )
42  : std::runtime_error( what_arg )
43  { }
44 };
45 
46 struct Number
47 {
48  union {
49  float f;
50  int i;
51  };
52 
53  Number(const float v) : f(v) {}
54  Number(const int v) : i(v) {}
55 };
56 
57 typedef std::vector<Number> NumVec;
58 typedef std::vector<std::string> StringVec;
59 
60 enum ValueType {
61  NULL_TYPE = 0,
62  STRING_TYPE = 1,
63  INT_TYPE = 2,
64  BOOL_TYPE = 3,
65  FLOAT_TYPE = 4,
66  POINT_TYPE = 5,
67  NORMAL_TYPE = 6,
68  VECTOR_TYPE = 7,
69  RGB_TYPE = 8,
70  XYZ_TYPE = 9,
71  SPECTRUM_TYPE = 10,
72 };
73 
74 struct Value
75 {
76  uint32 type;
77  union {
78  NumVec *nvec;
79  StringVec *svec;
80  };
81 
82  Value() : type(NULL_TYPE) {}
83  ~Value()
84  {
85  if (type == STRING_TYPE)
86  delete svec;
87  else if (type != NULL_TYPE)
88  delete nvec;
89  }
90 
91  Value(const Value& other)
92  {
93  type = other.type;
94  switch (other.type)
95  {
96  case NULL_TYPE:
97  break;
98  case STRING_TYPE:
99  svec = new StringVec(*other.svec);
100  break;
101  default:
102  nvec = new NumVec(*other.nvec);
103  break;
104  }
105  }
106 
107  size_t size() const
108  {
109  return (type == STRING_TYPE) ? svec->size() : nvec->size();
110  }
111  const float* get_floats() const
112  {
113  return reinterpret_cast<const float*>(&(*nvec)[0].f);
114  }
115  const int* get_ints() const
116  {
117  return reinterpret_cast<const int*>(&(*nvec)[0].i);
118  }
119  const std::string& get_string(const size_t i) const
120  {
121  return (*svec)[i];
122  }
123  float get_float(const size_t i) const
124  {
125  return (*nvec)[i].f;
126  }
127  int get_int(const size_t i) const
128  {
129  return (*nvec)[i].i;
130  }
131  bool get_bool(const size_t i) const
132  {
133  return (*nvec)[i].i ? true : false;
134  }
135 
136  void clear()
137  {
138  if (type == STRING_TYPE)
139  delete svec;
140  else if (type != NULL_TYPE)
141  delete nvec;
142 
143  type = NULL_TYPE;
144  }
145 
146  void set_type(const ValueType _type)
147  {
148  clear();
149 
150  type = _type;
151  if (type == STRING_TYPE)
152  svec = new StringVec();
153  else if (type != NULL_TYPE)
154  nvec = new NumVec();
155  }
156 
157  const char* type_string() const
158  {
159  switch (type)
160  {
161  case STRING_TYPE:
162  return "string";
163  case INT_TYPE:
164  return "integer";
165  case BOOL_TYPE:
166  return "bool";
167  case FLOAT_TYPE:
168  return "float";
169  case POINT_TYPE:
170  return "point";
171  case NORMAL_TYPE:
172  return "normal";
173  case VECTOR_TYPE:
174  return "vector";
175  case RGB_TYPE:
176  return "rgb";
177  case XYZ_TYPE:
178  return "xyz";
179  case SPECTRUM_TYPE:
180  return "spectrum";
181  }
182  return "null";
183  }
184 };
185 
187 {
188  std::vector<std::string> names;
189  std::vector<Value> values;
190 
191  void clear()
192  {
193  names.erase(names.begin(), names.end());
194  values.erase(values.begin(), values.end());
195  }
196 };
197 
198 struct Importer
199 {
200  virtual void identity() {}
201  virtual void transform(const Value& floats) {}
202  virtual void rotate(const float angle, const float x, const float y, const float z) {}
203  virtual void scale(const float x, const float y, const float z) {}
204  virtual void translate(const float x, const float y, const float z) {}
205  virtual void look_at(
206  const float ex, const float ey, const float ez,
207  const float lx, const float ly, const float lz,
208  const float ux, const float uy, const float uz) {}
209 
210  virtual void integrator(const char* name, const ParameterList& params) {}
211  virtual void sampler(const char* name, const ParameterList& params) {}
212  virtual void pixel_filter(const char* name, const ParameterList& params) {}
213  virtual void film(const char* name, const ParameterList& params) {}
214  virtual void camera(const char* name, const ParameterList& params) {}
215 
216  virtual void world_begin() {}
217  virtual void world_end() {}
218 
219  virtual void attribute_begin() {}
220  virtual void attribute_end() {}
221 
222  virtual void transform_begin() {}
223  virtual void transform_end() {}
224 
225  virtual void texture(const char* name, const char* texel_type, const char* texture_type, const ParameterList& params) {}
226  virtual void make_named_medium(const char* name, const ParameterList& params) {}
227  virtual void make_named_material(const char* name, const ParameterList& params) {}
228  virtual void named_material(const char* name) {}
229  virtual void medium_interface(const char* name1, const char* name2) {}
230  virtual void material(const char* name, const ParameterList& params) {}
231  virtual void area_light_source(const char* type, const ParameterList& params) {}
232 
233  virtual void shape(const char* type, const ParameterList& params) {}
234 };
235 
236 struct EchoImporter : public Importer
237 {
238  EchoImporter(FILE* _file) : stack_depth(0), file(_file) {};
239 
240  void indent()
241  {
242  for (unsigned i = 0; i < stack_depth; ++i)
243  fprintf(file, "\t");
244  }
245 
246  void print_value(const Value& value)
247  {
248  fprintf(file, "[");
249  if (value.type == STRING_TYPE)
250  {
251  for (size_t i = 0; i < value.size(); ++i)
252  fprintf(file, " \"%s\"", value.get_string(i).c_str());
253  }
254  else if (
255  value.type == FLOAT_TYPE ||
256  value.type == POINT_TYPE ||
257  value.type == NORMAL_TYPE ||
258  value.type == VECTOR_TYPE ||
259  value.type == RGB_TYPE ||
260  value.type == XYZ_TYPE ||
261  value.type == SPECTRUM_TYPE)
262  {
263  for (size_t i = 0; i < value.size(); ++i)
264  fprintf(file, " %f", value.get_float(i));
265  }
266  else if (value.type == INT_TYPE)
267  {
268  for (size_t i = 0; i < value.size(); ++i)
269  fprintf(file, " %i", value.get_int(i));
270  }
271  else if (value.type == BOOL_TYPE)
272  {
273  for (size_t i = 0; i < value.size(); ++i)
274  fprintf(file, " %s", value.get_bool(i) ? "\"true\"" : "\"false\"");
275  }
276  fprintf(file, " ]");
277  }
278  void print_params(const ParameterList& params)
279  {
280  for (size_t i = 0; i < params.names.size(); ++i)
281  {
282  fprintf(file, " \"%s %s\" ", params.values[i].type_string(), params.names[i].c_str());
283  print_value(params.values[i]);
284  }
285  }
286 
287  virtual void identity() { indent(); fprintf(file,"Identity\n"); }
288  virtual void transform(const Value& floats) { indent(); fprintf(file,"Transform "); print_value(floats); fprintf(file,"\n"); }
289  virtual void rotate(const float angle, const float x, const float y, const float z) { indent(); fprintf(file, "Rotate %f %f %f %f\n", angle, x, y, z); }
290  virtual void scale(const float x, const float y, const float z) { indent(); fprintf(file, "Scale %f %f %f\n", x, y, z); }
291  virtual void translate(const float x, const float y, const float z) { indent(); fprintf(file, "Translate %f %f %f\n", x, y, z); }
292  virtual void look_at(
293  const float ex, const float ey, const float ez,
294  const float lx, const float ly, const float lz,
295  const float ux, const float uy, const float uz)
296  {
297  fprintf(file, "LookAt %f %f %f %f %f %f %f %f %f\n", ex, ey, ez, lx, ly, lz, ux, uy, uz);
298  }
299 
300  virtual void integrator(const char* name, const ParameterList& params) { indent(); fprintf(file,"Integrator \"%s\" ", name); print_params(params); fprintf(file,"\n"); }
301  virtual void sampler(const char* name, const ParameterList& params) { indent(); fprintf(file,"Sampler \"%s\" ", name); print_params(params); fprintf(file,"\n"); }
302  virtual void pixel_filter(const char* name, const ParameterList& params) { indent(); fprintf(file,"PixelFilter \"%s\" ", name); print_params(params); fprintf(file,"\n"); }
303  virtual void film(const char* name, const ParameterList& params) { indent(); fprintf(file,"Film \"%s\" ", name); print_params(params); fprintf(file,"\n"); }
304  virtual void camera(const char* name, const ParameterList& params) { indent(); fprintf(file,"Camera \"%s\" ", name); print_params(params); fprintf(file,"\n"); }
305 
306  virtual void world_begin() { indent(); fprintf(file,"WorldBegin\n"); ++stack_depth; }
307  virtual void world_end() { --stack_depth; indent(); fprintf(file,"WorldEnd\n"); }
308 
309  virtual void attribute_begin() { indent(); fprintf(file,"AttributeBegin\n"); ++stack_depth; }
310  virtual void attribute_end() { --stack_depth; indent(); fprintf(file,"AttributeEnd\n"); }
311 
312  virtual void transform_begin() { indent(); fprintf(file,"TransformBegin\n"); ++stack_depth; }
313  virtual void transform_end() { --stack_depth; indent(); fprintf(file,"TransformEnd\n"); }
314 
315  virtual void texture(const char* name, const char* texel_type, const char* texture_type, const ParameterList& params)
316  {
317  indent();
318  fprintf(file,"Texture \"%s\" \"%s\" \"%s\" ", name, texel_type, texture_type);
319  print_params(params);
320  fprintf(file,"\n");
321  }
322  virtual void make_named_medium(const char* name, const ParameterList& params) { indent(); fprintf(file, "MakeNamedMedium \"%s\" ", name); print_params(params); fprintf(file,"\n"); }
323  virtual void make_named_material(const char* name, const ParameterList& params) { indent(); fprintf(file, "MakeNamedMaterial \"%s\"", name); print_params(params); fprintf(file,"\n"); }
324  virtual void named_material(const char* name) { indent(); fprintf(file, "NamedMaterial \"%s\"\n", name); }
325  virtual void medium_interface(const char* name1, const char* name2) { indent(); fprintf(file, "MediumInterface \"%s\" \"%s\"\n", name1, name2); }
326  virtual void material(const char* type, const ParameterList& params) { indent(); fprintf(file, "Material \"%s\" ", type); print_params(params); fprintf(file,"\n"); }
327  virtual void area_light_source(const char* type, const ParameterList& params) { indent(); fprintf(file, "AreaLightSource \"%s\" ", type); print_params(params); fprintf(file,"\n"); }
328 
329  virtual void shape(const char* type, const ParameterList& params) { indent(); fprintf(file, "Shape \"%s\" ", type); print_params(params); fprintf(file,"\n"); }
330 
331  unsigned stack_depth;
332  FILE* file;
333 };
334 
335 void import(const char* filename, Importer* importer);
336 
337 } // namespace pbrt
void transform(const uint32 n, const Iterator in, const Output out, const Functor functor)
Definition: primitives_inl.h:357
Definition: pbrt_parser.h:198
CUGAR_HOST_DEVICE Matrix< T, 4, 4 > translate(const Vector< T, 3 > &vec)
Definition: matrix_inline.h:524
Definition: pbrt_importer.cpp:41
Definition: pbrt_parser.h:38
Matrix< T, 4, 4 > look_at(const Vector< T, 3 > &eye, const Vector< T, 3 > &center, const Vector< T, 3 > &up, bool flip_sign=false)
Definition: matrix_inline.h:568
Definition: pbrt_parser.h:186
Definition: pbrt_parser.h:46
Definition: pbrt_parser.h:236
Definition: pbrt_parser.h:74
CUGAR_HOST_DEVICE Matrix< T, 4, 4 > scale(const Vector< T, 3 > &vec)
build a 3d scaling matrix
Definition: matrix_inline.h:539