Lumiverse  2.5
A framework for creating lighting control applications
ArnoldInterface.h
Go to the documentation of this file.
1 
4 #ifndef _ArnoldINTERFACE_H_
5 #define _ArnoldINTERFACE_H_
6 
7 #pragma once
8 
9 #include "LumiverseCoreConfig.h"
10 
11 #include <algorithm>
12 #include <cstdio>
13 
14 #ifdef USE_ARNOLD
15  #include "../lib/arnold/include/ai.h"
16 #endif
17 
18 #include "../Patch.h"
19 #include "../lib/libjson/libjson.h"
20 #include "ArnoldParameterVector.h"
21 #include <thread>
22 #include <iostream>
23 #include <locale>
24 #include <set>
25 
26 namespace Lumiverse {
27 #ifdef USE_ARNOLD
28 
29  struct ArnoldParam {
30  ArnoldParam()
31  : dimension(1), arnoldTypeName("unknown") { }
32 
33  size_t dimension;
34  std::string arnoldTypeName;
35  };
36 
38  template<typename T>
39  union AiNodeSetter {
40  void (*AiNodeSetter1D)(AtNode *, const char *, T);
41  void (*AiNodeSetter2D)(AtNode *, const char *, T, T);
42  void (*AiNodeSetter3D)(AtNode *, const char *, T, T, T);
43  void (*AiNodeSetter4D)(AtNode *, const char *, T, T, T, T);
44  };
45 #endif
46 
49  int bucket_xo;
50  int bucket_yo;
51  int bucket_size_x;
52  int bucket_size_y;
53 
55  bucket_xo(-1), bucket_yo(-1), bucket_size_x(-1), bucket_size_y(-1) { }
56  };
57 
59  struct ProgressInfo {
60  int bucket_sum;
61  int bucket_cur;
62 
63  ProgressInfo() : bucket_sum(-1), bucket_cur(-1) { }
64  };
65 
74  {
75  public:
82  ArnoldInterface() : m_buffer(NULL), m_gamma(2.2f), m_samples(-3), m_predictive(false),
83  m_bucket_pos(NULL), m_bucket_num(0), m_open(false) { }
84 
88  virtual ~ArnoldInterface() {
89  delete[] m_buffer;
90  m_buffer = NULL;
91  delete[] m_bucket_pos;
92  m_bucket_pos = NULL;
93  m_bucket_num = 0;
94  }
95 
101  virtual void init();
102 
109  virtual void init(const JSONNode jsonPatch) { this->init(); }
110 
114  virtual void close();
115 
121  virtual string getInterfaceType() { return "ArnoldInterface"; }
122 
128  int getWidth() { return m_width; }
129 
135  int getHeight() { return m_height; }
136 
140  virtual bool setDims(int w, int h);
141 
147  virtual float *getBufferPointer() { return m_buffer; }
148 
149 #ifdef USE_ARNOLD
150 
153  void setParameter(string lightName, string param, int val);
154 
158  void setParameter(string lightName, string param, float val);
159 
163  void setParameter(string lightName, string param, Eigen::Matrix3f rot, Eigen::Vector3f trans);
164 
168  void setParameter(string lightName, string param, float x, float y, float z);
169 
180  void setParameter(AtNode *light_ptr, const std::string &paramName, const std::string &value);
181 
191  void setArrayParameter(AtNode *light_ptr, const std::string &paramName, const std::string &value);
192 
196  virtual void setOptionParameter(const std::string &paramName, int val);
197  virtual void setOptionParameter(const std::string &paramName, float val);
198 
202  int getOptionParameter(const std::string &paramName);
203 
209  void loadArnoldParam(const JSONNode data);
210 #endif
211 
217  void setAssFile(std::string fileName) { m_ass_file = fileName; }
218 
224  std::string getAssFile() { return m_ass_file; }
225 
231  void setPluginDirectory(std::string dir) { m_plugin_dir = dir; }
232 
238  std::string getPluginDirectory() { return m_plugin_dir; }
239 
245  void setGamma(float gamma) { m_gamma = gamma; }
246 
252  float getGamma() { return m_gamma; }
253 
259  void setPredictive(bool predictive) { m_predictive = predictive; }
260 
266  bool getPredictive() { return m_predictive; }
267 
275  virtual void setSamples(int samples);
276 
282  int getSamples() { return m_samples; }
283 
289  virtual int render();
290 
300  virtual int render(const std::set<Device*>& /* devices */) { return this->render(); }
301 
305  virtual void interrupt();
306 
311  virtual JSONNode toJSON();
312 
317  virtual float getPercentage() {
318  if (m_progress.bucket_sum < 0)
319  return 0.f;
320  return (0.f + m_progress.bucket_cur) / m_progress.bucket_sum * 100.f;
321  }
322 
327  BucketPositionInfo *getBucketPositionInfo() const { return m_bucket_pos; }
328 
334  size_t getBucketNumber() const { return m_bucket_num; }
335 
336 #ifdef USE_ARNOLD
337 
341  void updateSurfaceColor(Eigen::Vector3d white);
342 #endif
343 
349  void setDefaultPath(std::string def_path) { m_default_path = def_path; }
350 
356  std::string getDefaultPath() { return m_default_path; }
357 
363  bool isOpen() { return m_open; }
364 
365 #ifdef USE_ARNOLD
366  void addGobo(AtNode *light_ptr, std::string file, float deg, float rot);
367 
371  void setLogFileName(string filename, int flags = AI_LOG_ALL);
372 
376  map<string, AtNode*> getLights();
377 
381  void setDriverFileName(string base, string filename);
382 #endif
383 
388  virtual bool isDistributedOpen();
389 
393  bool isUsingCaching() { return m_using_caching; }
394 
398  void setUsingCaching(bool usingCaching) { m_using_caching = usingCaching; }
399 
404  virtual void loadIfUsingCaching(const set<Device*>& /* devices */) { };
405 
406  protected:
407 #ifdef USE_ARNOLD
408 
418  template<size_t D, typename T>
419  void setSingleParameter(AtNode *node, const std::string &paramName, const std::string &value,
420  union AiNodeSetter<T> aiNodeSetter) const;
421 
433  template<size_t D, typename T, class C>
434  void setArrayParameter(AtNode *node, const std::string &paramName, const std::string &value,
435  bool (*AiArraySet) (AtArray*, AtUInt32, C, const char*, int), const int AiType) const;
436 
442  void appendToOutputs(const std::string buffer_output);
443 #endif
444 
450  inline static bool isRelativeFileName(std::string str) {
451  // This function is applied to STRING arnold parameters.
452  // Right now we only need to handle .ies file.
453  // May add more conditions to this line in the future.
454  std::transform(str.begin(), str.end(), str.begin(), ::tolower);
455  if ((!str.empty() && str[0] == '.') ||
456  ((str.find(".ies") != std::string::npos || str.find(".ass") != std::string::npos
457  || str.find(".jpg") != std::string::npos) &&
458  (str.find("/") == std::string::npos && str.find("\\") == std::string::npos)))
459  return true;
460  return false;
461  }
462 
468  inline std::string toRelativePath(std::string file);
469 
470 #ifdef USE_ARNOLD
471 
475  map<string, ArnoldParam> m_arnold_params;
476 #endif
477 
481  std::string m_ass_file;
482 
486  std::string m_plugin_dir;
487 
491  float *m_buffer;
492 
496  int m_width;
497 
501  int m_height;
502 
506  float m_gamma;
507 
512 
517 
521  bool m_open;
522 
523  // An array for worker threads.
524  BucketPositionInfo *m_bucket_pos;
525 
526  // The size of buckets array.
527  size_t m_bucket_num;
528 
529  // The progress info of the current frame.
530  ProgressInfo m_progress;
531 
532  // The default path for all passed files (.ass, .ies...)
533  std::string m_default_path;
534 
535  // Buffer driver name
536  string m_bufDriverName;
537 
544  };
545 
553  template<size_t D, typename T>
554  static bool parseArnoldParameter(const std::string &value, ArnoldParameterVector<D, T> &vector) {
555  T element;
556  std::string value_spaceless = value;
557  bool ret = true;
558 
559  // Removes spaces when the input type is not string
560  int count = 0;
561  for (char c : value) {
562  std::locale loc;
563  if (!std::isspace(c, loc)) {
564  value_spaceless[count++] = c;
565  }
566  }
567  value_spaceless = value_spaceless.substr(0, count);
568 
569  // Format: "v1, v2, ..."
570  size_t offset = 0;
571  size_t i;
572  for (i = 0; i < D; i++) {
573  std::istringstream iss(value_spaceless.substr(offset));
574  iss >> element;
575  vector[i] = element;
576 
577  offset = value_spaceless.find(",", offset);
578 
579  if (offset == std::string::npos) {
580  i++;
581  break;
582  }
583 
584  offset++;
585  }
586 
587  if (i != D) {
588  ret = false;
589  Logger::log(WARN, "Input dimension disagrees with vector dimension. Extra elements are left with existing values.");
590  }
591 
592  return ret;
593  }
594 }
595 
596 #endif
std::string getAssFile()
Gets the path to ass file.
Definition: ArnoldInterface.h:224
virtual JSONNode toJSON()
Parses the arnold parameter map to a JSON node.
Definition: ArnoldInterface.cpp:569
void setAssFile(std::string fileName)
Sets the path to ass file.
Definition: ArnoldInterface.h:217
bool getPredictive()
Gets the predictive flag.
Definition: ArnoldInterface.h:266
bool m_open
If the interface is currently open.
Definition: ArnoldInterface.h:521
virtual void interrupt()
Interrupts current rendering.
Definition: ArnoldInterface.cpp:560
Definition: ArnoldPatch copy.h:48
void setPredictive(bool predictive)
Sets the predictive flag.
Definition: ArnoldInterface.h:259
void setPluginDirectory(std::string dir)
Sets the directory containing the plugin (buffer_driver).
Definition: ArnoldInterface.h:231
int m_height
The height of the result.
Definition: ArnoldInterface.h:501
bool m_predictive
If turn on (so-called) predictive rendering.
Definition: ArnoldInterface.h:516
Wrapper for progress information computed based on number of buckets.
Definition: ArnoldInterface.h:59
int m_samples
Arnold AA samples.
Definition: ArnoldInterface.h:511
float getGamma()
Gets the gamma.
Definition: ArnoldInterface.h:252
std::string m_plugin_dir
The directory containing the plugin (buffer_driver).
Definition: ArnoldInterface.h:486
virtual int render()
Starts rendering with Arnold. Returns the error code of AiRender, so the caller can know if the rende...
Definition: ArnoldInterface.cpp:536
int getHeight()
Gets the height of result.
Definition: ArnoldInterface.h:135
ArnoldInterface()
Constructs a ArnoldInterface object.
Definition: ArnoldInterface.h:82
std::string toRelativePath(std::string file)
Converts to relative path with respect to json path. Currently it only checks if the file contains "...
Definition: ArnoldInterface.cpp:352
virtual int render(const std::set< Device * > &)
Fire off a render request using the given set of devices.
Definition: ArnoldInterface.h:300
virtual bool isDistributedOpen()
Indicates if the distributed interface is currently open In the base class, this is the same as the l...
Definition: ArnoldInterface.cpp:430
float m_gamma
The gamma for gamma correction.
Definition: ArnoldInterface.h:506
virtual void close()
Closes the Arnold session.
Definition: ArnoldInterface.cpp:507
virtual string getInterfaceType()
Gets the type of this object.
Definition: ArnoldInterface.h:121
static bool isRelativeFileName(std::string str)
Checks to see if this arnold STRING parameter is a file. Currently it only checks if the file contain...
Definition: ArnoldInterface.h:450
bool isUsingCaching()
Check whether this patch has caching enabled.
Definition: ArnoldInterface.h:393
virtual float getPercentage()
Gets the progress of current frame as a percentage.
Definition: ArnoldInterface.h:317
void setDefaultPath(std::string def_path)
Sets the default path.
Definition: ArnoldInterface.h:349
virtual void loadIfUsingCaching(const set< Device * > &)
If the interface is caching it should use this function to perform any preload operations it may be d...
Definition: ArnoldInterface.h:404
virtual void init()
Initializes the Arnold renderer.
Definition: ArnoldInterface.cpp:435
bool isOpen()
Checks if this interface is currently open.
Definition: ArnoldInterface.h:363
BucketPositionInfo * getBucketPositionInfo() const
Gets the current bucket for each worker thread.
Definition: ArnoldInterface.h:327
Stores a vector used by ArnoldPatch.
Defines a vector type for Arnold parameters, like color, vector etc.
Definition: ArnoldParameterVector.h:23
void log(LOG_LEVEL level, string message)
Logs a meesage to the output file.
Definition: Logger.cpp:46
bool m_using_caching
Is this interface using caching – currently only used for notifying the distributed renderer that it...
Definition: ArnoldInterface.h:543
Contains all core Lumiverse functions and variables.
Definition: Device.cpp:2
int getSamples()
Gets the sampling rate.
Definition: ArnoldInterface.h:282
size_t getBucketNumber() const
Gets number of buckets rendered simultanously. This is usually the number of threads supported by har...
Definition: ArnoldInterface.h:334
std::string getDefaultPath()
Gets the default path.
Definition: ArnoldInterface.h:356
Wrapper for unit (bucket) being rendered.
Definition: ArnoldInterface.h:48
float * m_buffer
The pointer to the frame buffer.
Definition: ArnoldInterface.h:491
void setUsingCaching(bool usingCaching)
Set whether this interface is using caching.
Definition: ArnoldInterface.h:398
static bool parseArnoldParameter(const std::string &value, ArnoldParameterVector< D, T > &vector)
Parses a formatted string into a ArnoldParameterVector instance.
Definition: ArnoldInterface.h:554
virtual float * getBufferPointer()
Gets the pointer to the frame buffer.
Definition: ArnoldInterface.h:147
std::string getPluginDirectory()
Gets the directory containing the plugin (buffer_driver).
Definition: ArnoldInterface.h:238
Definition: Logger.h:25
virtual bool setDims(int w, int h)
Sets the dimensions of the image.
Definition: ArnoldInterface.cpp:140
virtual void setSamples(int samples)
Sets the camera sampling rate used for current rendering.
Definition: ArnoldInterface.cpp:527
Interface between ArnoldPatch and arnold. Almost all arnold APIs are called from this class...
Definition: ArnoldInterface.h:73
virtual ~ArnoldInterface()
Destroys the object.
Definition: ArnoldInterface.h:88
int getWidth()
Gets the width of result.
Definition: ArnoldInterface.h:128
int m_width
The width of the result.
Definition: ArnoldInterface.h:496
std::string m_ass_file
The path to ass file.
Definition: ArnoldInterface.h:481
void setGamma(float gamma)
Sets the gamma.
Definition: ArnoldInterface.h:245
virtual void init(const JSONNode jsonPatch)
Initialize the ArnoldRenderer with a reference to a JSON serialized parent patch. ...
Definition: ArnoldInterface.h:109