Eidolon
RenderTypes.h
Go to the documentation of this file.
1 /*
2  * Eidolon Biomedical Framework
3  * Copyright (C) 2016-8 Eric Kerfoot, King's College London, all rights reserved
4  *
5  * This file is part of Eidolon.
6  *
7  * Eidolon is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * Eidolon is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program (LICENSE.txt). If not, see <http://www.gnu.org/licenses/>
19  */
20 
21 #ifndef RENDERTYPES_H
22 #define RENDERTYPES_H
23 
24 #include <cstdio>
25 #include <cstdlib>
26 #include <ctime>
27 #include <cerrno>
28 #include <string>
29 #include <fstream>
30 #include <sstream>
31 #include <iostream>
32 #include <cstring>
33 #include <cmath>
34 #include <vector>
35 #include <map>
36 #include <algorithm>
37 #include <utility>
38 #include <limits>
39 #include <stdexcept>
40 
41 #ifdef WIN32
42  typedef void* _WId;
43  #define WIN32_LEAN_AND_MEAN
44  #include <windows.h>
45  #include <tchar.h>
46  #ifdef RENDER_EXPORT
47  # define DLLEXPORT __declspec( dllexport )
48  #else
49  # define DLLEXPORT __declspec( dllimport )
50  #endif
51  #define PLATFORM_ID "Windows"
52  #pragma warning(disable:4244) // disable double->float conversion complaint
53  #pragma warning(disable:4250) // disable inheritance via dominance complaint
54  #pragma warning(disable:4996) // disable strerror warning
55  #pragma warning(disable:4100) // disable unreferenced formal parameter warning
56 #elif defined(__APPLE__)
57  #include <unistd.h>
58  #include <pthread.h>
59  #include <sys/mman.h>
60  #include <sys/stat.h>
61  #include <sys/time.h>
62  #include <sys/posix_shm.h>
63  #include <fcntl.h>
64  #define MAXSHMNAMLEN PSHMNAMLEN
65  #define DLLEXPORT
66  #define PLATFORM_ID "OSX"
67  typedef long _WId;
68 #else
69  #include <unistd.h>
70  #include <pthread.h>
71  #include <sys/mman.h>
72  #include <sys/stat.h>
73  #include <fcntl.h>
74  #include <limits.h>
75  #define MAXSHMNAMLEN NAME_MAX
76  #define DLLEXPORT
77  #define PLATFORM_ID "Linux"
78  typedef unsigned long _WId;
79 #endif
80 
81 #define dPI 3.141592653589793238462
82 #define fPI 3.141592f
83 #define fEPSILON 1.0e-10f
84 #define dEPSILON 1.0e-10
85 
86 #define _HASH(h,v,s) (((h)<<(s)|((h)>>((sizeof(h)<<3)-(s))))^(v))
87 
88 #define DBGOUT(c) do { std::cout << c << std::endl; std::cout.flush(); } while(0)
89 
90 #define SAFE_DELETE(p) do { if((p)!=NULL){ delete (p); (p)=NULL; } } while(0)
91 
92 namespace RenderTypes {
93 
94 // various string names
95 static const char* platformID=PLATFORM_ID;
96 static const char* parentPIDVar="PARENTPID";
97 static const char* RenderParamGroup="RenderParam";
98 
99 // platform-independent basic type definitions
100 typedef int i32;
101 typedef long long i64;
102 typedef unsigned char u8;
103 typedef unsigned int u32;
104 typedef unsigned long long u64;
105 
106 // specific type definitions which are meaningful for data structures and the rendering systems
107 typedef u32 sval; // size value, fixed at 32bits even on 64bit platforms
108 typedef double real; // real value data type for internal code and file formats
109 typedef u32 rgba; // 32bit color data type
110 typedef u32 indexval; // index value data type for internal code and file formats, may differ from sval in future if larger indices are needed
111 
112 static const real realInf=std::numeric_limits<real>::infinity();
113 
114 template<typename T> T _min(const T& a, const T& b) { return a<b ? a : b; }
115 template<typename T> T _max(const T& a, const T& b) { return a>b ? a : b; }
116 
117 template<typename T> T clamp(const T& val, const T& minval, const T& maxval)
118 {
119  if(val>maxval)
120  return maxval;
121  if(val<minval)
122  return minval;
123 
124  return val;
125 }
126 
127 template<typename T> T lerpXi(const T& val, const T& minv, const T& maxv)
128 {
129  return minv==maxv ? val : (val-minv)/(maxv-minv);
130 }
131 
132 template<typename V,typename T> T lerp(const V& val, const T& v1, const T& v2)
133 {
134  return v1+(v2-v1)*val;
135 }
136 
137 template<typename T> int compT( const T& t1, const T& t2)
138 {
139  if(t2<t1)
140  return 1;
141  if(t1<t2)
142  return -1;
143 
144  return 0;
145 }
146 
147 template<typename T> int compV(const void* t1, const void* t2)
148 {
149  return compT<T>(*((T*)t1),*((T*)t2));
150 }
151 
152 template<typename T> int sortTupleFirstCB(const void* v1, const void* v2)
153 {
154  return compT(((T*)v1)->first,((T*)v2)->first);
155 }
156 
157 template<typename T> int sortTupleSecondCB(const void* v1, const void* v2)
158 {
159  return compT(((T*)v1)->second,((T*)v2)->second);
160 }
161 
162 template<typename T> int sortTupleThirdCB(const void* v1, const void* v2)
163 {
164  return compT(((T*)v1)->third,((T*)v2)->third);
165 }
166 
167 template<typename T> int sortTupleFourthCB(const void* v1, const void* v2)
168 {
169  return compT(((T*)v1)->fourth,((T*)v2)->fourth);
170 }
171 
173 inline bool equalsEpsilon(real v1, real v2)
174 {
175  return fabs(v1-v2)<=dEPSILON;
176 }
177 
179 inline bool isNan(real v)
180 {
181  volatile real vv=v; // must be volatile to prevent optimizations
182  return vv!=vv; // NaN is the only value not equal to itself
183 }
184 
185 inline real frand()
186 {
187  return real(rand())/real(RAND_MAX);
188 }
189 
190 inline real fround(real r)
191 {
192  return floor(0.5+r);
193 }
194 
195 inline std::string getPIDStr()
196 {
197 #ifdef WIN32
198  DWORD self=GetCurrentProcessId();
199 #else
200  pid_t self=getpid();
201 #endif
202 
203  std::ostringstream out;
204  out << self;
205  return out.str();
206 }
207 
208 inline std::string getPPIDStr()
209 {
210 #ifdef WIN32
211  DWORD self=0;
212 #else
213  pid_t self=getppid();
214 #endif
215  std::ostringstream out;
216  out << self;
217  return out.str();
218 }
219 
220 inline bool isParentProc()
221 {
222  const char* parentpid=getenv(parentPIDVar);
223  return parentpid==NULL || std::string(parentpid)==getPIDStr();
224 }
225 
226 template<typename T> T swapEndianN(T t)
227 {
228  union {T v; u8 b[sizeof(T)]; } src,dst;
229  src.v=t;
230  for(size_t x=0;x<sizeof(T);x++)
231  dst.b[x]=src.b[sizeof(T)-x-1];
232  return dst.v;
233 }
234 
235 template<typename T> T swapEndian32(T t)
236 {
237  union {T v; u8 b[4]; } src,dst;
238  src.v=t;
239  dst.b[0]=src.b[3];
240  dst.b[1]=src.b[2];
241  dst.b[2]=src.b[1];
242  dst.b[3]=src.b[0];
243  return dst.v;
244 }
245 
246 template<typename T> T swapEndian64(T t)
247 {
248  union {T v; u8 b[8]; } src,dst;
249  src.v=t;
250  dst.b[0]=src.b[7];
251  dst.b[1]=src.b[6];
252  dst.b[2]=src.b[5];
253  dst.b[3]=src.b[4];
254  dst.b[4]=src.b[3];
255  dst.b[5]=src.b[2];
256  dst.b[6]=src.b[1];
257  dst.b[7]=src.b[0];
258  return dst.v;
259 }
260 
261 template<typename F,typename S,typename T>
262 class triple
263 {
264 public:
265  F first;
267  T third;
268 
269  triple() : first(),second(),third() {}
270  triple(const F& first,const S& second, const T& third) : first(first),second(second), third(third) {}
271  triple(const triple<F,S,T>& t) : first(t.first),second(t.second),third(t.third) {}
272 };
273 
274 template<typename F,typename S,typename T,typename U>
276 {
277 public:
278  F first;
280  T third;
282 
283  quadruple() : first(),second(),third(), fourth() {}
284  quadruple(const F& first,const S& second, const T& third, const U& fourth) : first(first),second(second), third(third),fourth(fourth) {}
285  quadruple(const quadruple<F,S,T,U>& t) : first(t.first),second(t.second),third(t.third),fourth(t.fourth) {}
286 };
287 
288 typedef std::pair<indexval,indexval> indexpair;
289 typedef std::pair<real,real> realpair;
291 typedef std::pair<indexval,realtriple> indextriple;
293 
294 template<typename T>
295 void bswap(T& a, T& b)
296 {
297  u8 tmp,*aa=(u8*)&a,*bb=(u8*)&b;
298  for(size_t i=0;i<sizeof(T);i++){
299  tmp=aa[i];
300  aa[i]=bb[i];
301  bb[i]=tmp;
302  }
303 }
304 
307 {
308 public:
309  clock_t start,stop;
310  double delta;
311  bool doPrint;
312  bool entered;
313  std::string label;
314 
315  TimingObject(const std::string& label, bool doPrint=true) : delta(0),doPrint(doPrint), entered(false), label(label)
316  {
317 #ifdef WIN32
318  this->label=label.substr(label.find_last_of("::")+1);
319 #endif
320 
321  start=clock();
322  if(doPrint){
323  std::cout << this->label << std::endl;
324  std::cout.flush();
325  }
326  }
327 
328  ~TimingObject(){ stopTiming(); }
329 
330  void stopTiming()
331  {
332  stop=clock();
333  delta=double(stop-start)/CLOCKS_PER_SEC;
334  if(doPrint){
335  std::cout << label << " dT (s) = " << delta << std::endl;
336  std::cout.flush();
337  }
338  }
339 
340  bool loopOnce()
341  {
342  if(entered)
343  return false;
344 
345  entered=true;
346  return true;
347  }
348 };
349 
350 #define TIMING TimingObject __functimer(__FUNCTION__)
351 #define TIMINGBLOCK(label) for(TimingObject __blockobj(label);__blockobj.loopOnce();)
352 
353 #ifdef WIN32
354  #define MutexType CRITICAL_SECTION
355  #define trylock_mutex(m) (TryEnterCriticalSection(m)!=0)
356  #define lock_mutex EnterCriticalSection
357  #define unlock_mutex LeaveCriticalSection
358  #define destroy_mutex DeleteCriticalSection
359 #else
360  #define MutexType pthread_mutex_t
361  #define trylock_mutex(m) (pthread_mutex_trylock(m)==0)
362  #define lock_mutex pthread_mutex_lock
363  #define unlock_mutex pthread_mutex_unlock
364  #define destroy_mutex pthread_mutex_destroy
365 #endif
366 
368 class Mutex
369 {
371 
372 public:
373  class Locker
374  {
375  protected:
377  bool runblock;
378 
379  public:
380  Locker(Mutex* p, real timeout=0.0) : parent(p) { runblock=parent->lock(timeout); }
381 
382  ~Locker() { if(parent)parent->release(); }
383 
384  bool loopOnce()
385  {
386  if(!runblock)
387  return false;
388 
389  runblock=false;
390  return true;
391  }
392  };
393 
395  {
396 #ifdef WIN32
397  InitializeCriticalSection(&_mutex);
398 #else
399  pthread_mutexattr_t attrs;
400  pthread_mutexattr_init(&attrs);
401  pthread_mutexattr_settype(&attrs,PTHREAD_MUTEX_RECURSIVE);
402  pthread_mutex_init(&_mutex,&attrs);
403 #endif
404  }
405 
406  ~Mutex() { destroy_mutex(&_mutex); }
407 
412  bool lock(real timeout=0.0)
413  {
414  if(timeout>0){
415  clock_t start=clock();
416  real delta=0;
417  bool result=false;
418 
419  while(!result && delta<timeout){
420  result=trylock_mutex(&_mutex);
421  delta=real(clock()-start)/CLOCKS_PER_SEC;
422  }
423 
424  return result;
425  }
426  else{
427  lock_mutex(&_mutex);
428  return true;
429  }
430  }
431 
433  void release() { unlock_mutex(&_mutex); }
434 };
435 
436 #define critical(m) for(Mutex::Locker __locker__=Mutex::Locker(m);__locker__.loopOnce();)
437 #define trylock(m,timeout) for(Mutex::Locker __locker__=Mutex::Locker(m,timeout);__locker__.loopOnce();)
438 
439 
442 {
443  FT_LINELIST = 0, // list of line segments
444  FT_POINTLIST = 1, // list of discrete points
445  FT_TRILIST = 2, // list of triangles
446  FT_TRISTRIP = 3, // strip of triangles
447  FT_BB_POINT = 4, // BBT_POINT billboard points
448  FT_BB_FIXED_PAR = 5, // BBT_ORIENTED_SELF billboard oriented along an axis of rotation
449  FT_BB_FIXED_PERP = 6, // BBT_PERPENDICULAR_SELF billboard oriented along a normal of rotation
450  FT_GLYPH = 7, // list of points represented by glyph meshes
451 // FT_INTERPFIGURE = 8, // figure which interpolates between two given other figures
452  FT_RIBBON = 8, // list of lines represented by ribbons
453  FT_TEXVOLUME = 9, // 3D texture volume
454  FT_TEXT = 10 // text billboard
455 };
456 
460 {
466 };
467 
474 {
475  TF_RGB24, // 24-bit RGB pixels PF_R8G8B8
476  TF_RGBA32, // 32-bit RGBA pixels PF_R8G8B8A8
477  TF_ARGB32, // 32-bit ARGB pixels PF_A8R8G8B8
478  TF_LUM8, // 8-bit greyscale, no alpha PF_L8
479  TF_LUM16, // 16-bit greyscale, no alpha PF_L16
480  TF_ALPHA8, // 8-bit alpha mask, no greyscale PF_A8
481  TF_ALPHALUM8, // 4-bit alpha, 4-bit greyscale PF_A4L4
482  TF_UNKNOWN // Any other format from the renderer that's not used/understood
483 };
484 
486 {
490 };
491 
493 {
497 };
498 
500 {
504 };
505 
506 
507 
508 #ifdef WIN32
509  std::string formatLastErrorMsg(); // Windows error reporting helper
510 #endif
511 
512 // OS X shared memory cleanup stuff
513 
514 //extern std::vector<std::string> sharednamelist; // list of shared names to delete on exit
515 //void addShared(const std::string& name);
516 //void cleanupShared();
517 
518 
519 void initSharedDir(const std::string& path);
520 std::string getSharedDir();
521 void addShared(const std::string& name);
522 void unlinkShared(const std::string& name);
523 
524 /*****************************************************************************************************************************/
525 /* Math Objects */
526 /*****************************************************************************************************************************/
527 
529 class color
530 {
531  float _r,_g,_b,_a;
532 
533 public:
535  static rgba interpolate(real val,rgba left, rgba right)
536  {
537  u32 bf = u32(val*255);
538  u32 af = 255 - bf;
539 
540  u32 al = (left & 0x00ff00ff);
541  u32 ah = (left & 0xff00ff00) >> 8;
542  u32 bl = (right & 0x00ff00ff);
543  u32 bh = (right & 0xff00ff00) >> 8;
544 
545  u32 ml = (al * af + bl * bf);
546  u32 mh = (ah * af + bh * bf);
547 
548  return (mh & 0xff00ff00) | ((ml & 0xff00ff00) >> 8);
549  }
550 
551  color(float r=1.0f, float g=1.0f, float b=1.0f, float a=1.0f) : _r(r),_g(g),_b(b),_a(a)
552  {}
553 
554  color(const color & c) : _r(c.r()),_g(c.g()),_b(c.b()),_a(c.a())
555  {}
556 
557  color(const rgba& c) : _r(u8(c>>24)/255.0f), _g(u8(c>>16)/255.0f), _b(u8(c>>8)/255.0f), _a(u8(c)/255.0f)
558  {}
559 
560  float r() const { return _r; }
561  float g() const { return _g; }
562  float b() const { return _b; }
563  float a() const { return _a; }
564 
565  float r(float val) { _r=val; return _r; }
566  float g(float val) { _g=val; return _g; }
567  float b(float val) { _b=val; return _b; }
568  float a(float val) { _a=val; return _a; }
569 
570  void setBuff(float *v) const { v[0]=_r; v[1]=_g; v[2]=_b; v[3]=_a; }
571 
573  rgba toRGBA() const
574  {
575  rgba result=u8(_r*255);
576  result=(result<<8)|u8(_g*255);
577  result=(result<<8)|u8(_b*255);
578  result=(result<<8)|u8(_a*255);
579  return result;
580  }
581 
583  color interpolate(real val,const color& col) const
584  {
585  if(val>=1)
586  return col;
587 
588  if(val<=0)
589  return *this;
590 
591  real val1=1.0f-val;
592  return color(_r*val1+col.r()*val,_g*val1+col.g()*val,_b*val1+col.b()*val,_a*val1+col.a()*val);
593  }
594 
596  {
597  return color(clamp(_r,0.0f,1.0f),clamp(_g,0.0f,1.0f),clamp(_b,0.0f,1.0f),clamp(_a,0.0f,1.0f));
598  }
599 
600  bool operator == (const color & c) const { return equalsEpsilon(r(),c.r()) && equalsEpsilon(g(),c.g()) && equalsEpsilon(b(),c.b()) && equalsEpsilon(a(),c.a()); }
601  bool operator != (const color & c) const { return !((*this)==c); }
602 
603  color operator * (const color & c) const { return color(_r*c.r(),_g*c.g(),_b*c.b(),_a*c.a()); }
604  color operator * (real r) const { return color(_r*r,_g*r,_b*r,_a*r); }
605 
606  color operator + (const color & c) const { return color(_r+c.r(),_g+c.g(),_b+c.b(),_a+c.a()); }
607  color operator + (real r) const { return color(_r+r,_g+r,_b+r,_a+r); }
608 
609  color operator - (const color & c) const { return color(_r-c.r(),_g-c.g(),_b-c.b(),_a-c.a()); }
610  color operator - (real r) const { return color(_r-r,_g-r,_b-r,_a-r); }
611 
612  bool operator < (const color &c) const { return (_r-dEPSILON)<c.r() && (_g-dEPSILON)<c.g() && (_b-dEPSILON)<c.b() && (_a-dEPSILON)<c.a(); }
613  bool operator > (const color &c) const { return (_r+dEPSILON)>c.r() && (_g+dEPSILON)>c.g() && (_b+dEPSILON)>c.b() && (_a+dEPSILON)>c.a(); }
614 
615  friend std::ostream& operator << (std::ostream &out, const color &c)
616  {
617  return out << "color(" << c.r() << ", " << c.g() << ", " << c.b() << ", " << c.a() << ")";
618  }
619 };
620 
622 class vec3
623 {
624  real _x,_y,_z;
625 
626 public:
628  vec3(real val=0) : _x(val),_y(val),_z(val) {}
630  vec3(real x, real y, real z=0) : _x(x),_y(y),_z(z) {}
632  vec3(const vec3 & v) : _x(v.x()),_y(v.y()),_z(v.z()) {}
633 
634  real x() const { return _x; }
635  real y() const { return _y; }
636  real z() const { return _z; }
637 
638  real x(real v) { _x=v; return _x; }
639  real y(real v) { _y=v; return _y; }
640  real z(real v) { _z=v; return _z; }
641 
642  void setBuff(float *v) const { v[0]=_x; v[1]=_y; v[2]=_z; }
643 
644  vec3 operator + (const vec3& v) const { return vec3(_x+v.x(),_y+v.y(),_z+v.z()); }
645  vec3 operator - (const vec3& v) const { return vec3(_x-v.x(),_y-v.y(),_z-v.z()); }
646  vec3 operator * (const vec3& v) const { return vec3(_x*v.x(),_y*v.y(),_z*v.z()); }
647  vec3 operator / (const vec3& v) const { return vec3(_x/v.x(),_y/v.y(),_z/v.z()); }
648  vec3 operator + (real v) const { return vec3(_x+v,_y+v,_z+v); }
649  vec3 operator - (real v) const { return vec3(_x-v,_y-v,_z-v); }
650  vec3 operator * (real v) const { return vec3(_x*v,_y*v,_z*v); }
651  vec3 operator / (real v) const { return vec3(_x/v,_y/v,_z/v); }
652  vec3 operator - () const { return vec3(-_x,-_y,-_z); }
653 
655  vec3 abs() const { return vec3(fabs(_x),fabs(_y),fabs(_z)); }
657  vec3 inv() const { return vec3(_x!=0 ? 1/_x : 0,_y!=0 ? 1/_y : 0,_z!=0 ? 1/_z : 0); }
659  vec3 sign() const { return vec3(_x>=0 ? 1 : -1,_y>=0 ? 1 : -1,_z>=0 ? 1 : -1); }
661  vec3 cross(const vec3& v) const { return vec3(_y * v.z() - _z * v.y(), _z * v.x() - _x * v.z(), _x * v.y() - _y * v.x()); }
663  real dot(const vec3& v) const { return _x*v.x()+_y*v.y()+_z*v.z(); }
665  real len() const { return sqrt(_x*_x+_y*_y+_z*_z); }
667  real lenSq() const { return _x*_x+_y*_y+_z*_z; }
669  vec3 norm() const { real l=len(); return l==0.0 ? vec3() : (*this)*(1.0/l); }
671  real distTo(const vec3 & v) const { return ((*this)-v).len(); }
673  real distToSq(const vec3 & v) const { return ((*this)-v).lenSq(); }
675  vec3 clamp(const vec3& v1,const vec3& v2) const { return vec3(RenderTypes::clamp(_x,v1.x(),v2.x()),RenderTypes::clamp(_y,v1.y(),v2.y()),RenderTypes::clamp(_z,v1.z(),v2.z())); }
676 
678  void setMinVals(const vec3 &v) { _x=_min(_x,v.x()); _y=_min(_y,v.y()); _z=_min(_z,v.z()); }
680  void setMaxVals(const vec3 &v) { _x=_max(_x,v.x()); _y=_max(_y,v.y()); _z=_max(_z,v.z()); }
681 
683  void normThis() { real l=len(); if(l>0){ _x/=l;_y/=l;_y/=l;} }
684 
686  vec3 toPolar() const { real l=len(); return l==0.0 ? vec3() : vec3(atan2(_y,_x),acos(_z/l),l); }
687 
689  vec3 toCylindrical() const { return vec3(atan2(_y,_x),_z,sqrt(_y*_y+_x*_x)); }
690 
692  vec3 fromPolar() const { return vec3(cos(_x)*sin(_y)*_z,sin(_y)*sin(_x)*_z,cos(_y)*_z); }
693 
695  vec3 fromCylindrical() const { return vec3(cos(_x)*_z,sin(_x)*_z,_y); }
696 
698  bool isZero() const { return equalsEpsilon(_x,0) && equalsEpsilon(_y,0) && equalsEpsilon(_z,0); }
699 
701  bool inAABB(const vec3& minv, const vec3& maxv) const
702  {
703  //return (_x+dEPSILON)>=minv.x() && (_y+dEPSILON)>=minv.y() && (_z+dEPSILON)>=minv.z()
704  // && (_x-dEPSILON)<=maxv.x() && (_y-dEPSILON)<=maxv.y() && (_z-dEPSILON)<=maxv.z();
705  return *this>minv && *this<maxv;
706  }
707 
709  bool inOBB(const vec3& center, const vec3& hx, const vec3& hy, const vec3& hz) const
710  {
711  vec3 diff=(*this)-center;
712 
713  // perform 3 plane distance checks, return true if the distance from plane (center,hx) is less than hx.len(), etc
714  return fabs(hx.dot(diff))<=hx.lenSq() && fabs(hy.dot(diff))<=hy.lenSq() && fabs(hz.dot(diff))<=hz.lenSq();
715  }
716 
718  bool inSphere(const vec3& center,real radius) const { return distToSq(center)<=(radius*radius+dEPSILON); }
719 
721  bool onPlane(const vec3& planept, const vec3& planenorm) const { return equalsEpsilon(planeDist(planept,planenorm),0); }
722 
724  bool isInUnitCube(real margin=0.0) const { return _x>=-margin && _x<=(1.0+margin) && _y>=-margin && _y<=(1.0+margin) && _z>=-margin && _z<=(1.0+margin); }
725 
727  bool isParallel(const vec3 &other) const
728  {
729  return cross(other).isZero();
730  }
731 
733  bool operator == (const vec3 & v) const { return equalsEpsilon(_x,v.x()) && equalsEpsilon(_y,v.y()) && equalsEpsilon(_z,v.z()); }
734 
736  bool operator != (const vec3 &v) const { return !equalsEpsilon(_x,v.x()) || !equalsEpsilon(_y,v.y()) || !equalsEpsilon(_z,v.z()); }
737 
738  bool operator < (const vec3 &v) const { return (_x-dEPSILON)<v.x() && (_y-dEPSILON)<v.y() && (_z-dEPSILON)<v.z(); }
739  bool operator > (const vec3 &v) const { return (_x+dEPSILON)>v.x() && (_y+dEPSILON)>v.y() && (_z+dEPSILON)>v.z(); }
740 
741  int cmp(const vec3 &v) const
742  {
743  // the order of these statements defines the sorting order where Z is used to sort first and X last
744  if(_z<v.z()) return -1;
745  if(_z>v.z()) return 1;
746  if(_y<v.y()) return -1;
747  if(_y>v.y()) return 1;
748  if(_x<v.x()) return -1;
749  if(_x>v.x()) return 1;
750  return 0;
751  }
752 
754  real angleTo(const vec3 &v) const
755  {
756  real l=sqrt(lenSq() * v.lenSq());
757 
758  if(l<dEPSILON)
759  return 0.0;
760 
761  real vl=dot(v)/l;
762 
763  if(vl>=(1.0-dEPSILON))
764  return 0.0;
765 
766  if(vl<=(-1.0+dEPSILON))
767  return dPI;
768 
769  return acos(vl);
770  }
771 
773  vec3 planeNorm(const vec3& v2, const vec3& v3) const { return (v2-*this).cross(v3-*this).norm(); }
774 
776  vec3 planeNorm(const vec3& v2, const vec3& v3, const vec3& farv) const
777  {
778  vec3 norm=planeNorm(v2,v3);
779  return norm.angleTo(farv-*this)>=(dPI*0.5) ? norm : -norm;
780  }
781 
783  real planeDist(const vec3& planept, const vec3& planenorm) const { return planenorm.dot((*this)-planept); }
784 
786  vec3 planeProject(const vec3& planept, const vec3& planenorm) const { return (*this)-(planenorm*planeDist(planept,planenorm)); }
787 
789  int planeOrder(const vec3& planenorm,const vec3& v1,const vec3& v2) const
790  {
791  real order=(v1-*this).cross(v2-*this).dot(planenorm);
792  if(order>0)
793  return 1;
794  if(order<0)
795  return -1;
796  return 0;
797  }
798 
800  real triArea(const vec3& b, const vec3& c) const
801  {
802  vec3 bb=b-*this;
803  vec3 cc=c-*this;
804 
805  return bb.len()*cc.len()*sin(bb.angleTo(cc))*0.5;
806  }
807 
809  real lineDist(vec3 p1,vec3 p2) const
810  {
811  vec3 p=p2-p1;
812  real pl=p.len();
813  if(pl<dEPSILON) // p1==p2 so there's no line
814  return -1;
815 
816  if(planeDist(p1,p)<0 || planeDist(p2,-p)<0) // this is outside the cylinder area
817  return -1;
818 
819  return p.cross(p1-*this).len()/pl;
820  }
821 
823  vec3 lerp(real val,const vec3& v) const
824  {
825  return vec3(_x+(v.x()-_x)*val,_y+(v.y()-_y)*val,_z+(v.z()-_z)*val);
826  }
827 
828  i32 hash() const
829  {
830  union { real f; i64 i;} x,y,z;
831  x.f=_x;
832  y.f=_y;
833  z.f=_z;
834 
835  i64 hash=_HASH(x.i,_HASH(y.i,z.i,13),14);
836  return i32(hash>>32)^i32(hash);
837  }
838 
839  friend std::ostream& operator << (std::ostream &out, const vec3 &v)
840  {
841  return out << "vec3(" << v.x() << ", " << v.y() << ", " << v.z() << ")";
842  }
843 
844  static int compX(const void* v1, const void* v2)
845  {
846  return compT<real>(((const vec3*)v1)->x(),((const vec3*)v2)->x());
847  }
848 
849  static int compY(const void* v1, const void* v2)
850  {
851  return compT<real>(((const vec3*)v1)->y(),((const vec3*)v2)->y());
852  }
853 
854  static int compZ(const void* v1, const void* v2)
855  {
856  return compT<real>(((const vec3*)v1)->z(),((const vec3*)v2)->z());
857  }
858 
859  static vec3 posInfinity() { return vec3(realInf); }
860  static vec3 negInfinity() { return vec3(-realInf); }
861 
863  static vec3 X() { return vec3(1,0,0); }
864 
866  static vec3 Y() { return vec3(0,1,0); }
867 
869  static vec3 Z() { return vec3(0,0,1); }
870 };
871 
872 class mat4
873 {
874 public:
875  union {
876  struct{
877  real
878  m00,m01,m02,m03,
879  m10,m11,m12,m13,
880  m20,m21,m22,m23,
881  m30,m31,m32,m33;
882  };
883  real m[4][4];
884  };
885 
886  mat4() { clear(); }
887  mat4(const real* mat) { memcpy(m,mat,sizeof(real)*16); }
888  mat4(real m00,real m01,real m02,real m03, real m10,real m11,real m12,real m13, real m20,real m21,real m22,real m23, real m30,real m31,real m32,real m33) :
889  m00(m00),m01(m01),m02(m02),m03(m03), m10(m10),m11(m11),m12(m12),m13(m13), m20(m20),m21(m21),m22(m22),m23(m23), m30(m30),m31(m31),m32(m32),m33(m33) {}
890 
891  void clear() { memset(m,0,sizeof(real)*16); }
892  void ident() { clear(); m00=m11=m22=m33=1.0; }
893 
894  real* getPointer() const { return (real*)m; }
895 
896  vec3 operator * (const vec3& v) const
897  {
898  vec3 vv= vec3(m00*v.x() + m01*v.y() + m02*v.z() + m03,m10*v.x() + m11*v.y() + m12*v.z() + m13,m20*v.x() + m21*v.y() + m22*v.z() + m23);
899  real d=m30*v.x() + m31*v.y() + m32*v.z() + m33;
900  return d==0 ? vec3() : (vv/d);
901  }
902 
903  friend vec3 operator * (const vec3 &v,const mat4& m)
904  {
905  return m*v;
906  }
907 
908  mat4 operator * (const mat4& m) const
909  {
910  return mat4(
911  m.m00*m00 + m.m10*m01 + m.m20*m02 + m.m30*m03,
912  m.m01*m00 + m.m11*m01 + m.m21*m02 + m.m31*m03,
913  m.m02*m00 + m.m12*m01 + m.m22*m02 + m.m32*m03,
914  m.m03*m00 + m.m13*m01 + m.m23*m02 + m.m33*m03,
915  m.m00*m10 + m.m10*m11 + m.m20*m12 + m.m30*m13,
916  m.m01*m10 + m.m11*m11 + m.m21*m12 + m.m31*m13,
917  m.m02*m10 + m.m12*m11 + m.m22*m12 + m.m32*m13,
918  m.m03*m10 + m.m13*m11 + m.m23*m12 + m.m33*m13,
919  m.m00*m20 + m.m10*m21 + m.m20*m22 + m.m30*m23,
920  m.m01*m20 + m.m11*m21 + m.m21*m22 + m.m31*m23,
921  m.m02*m20 + m.m12*m21 + m.m22*m22 + m.m32*m23,
922  m.m03*m20 + m.m13*m21 + m.m23*m22 + m.m33*m23,
923  m.m00*m30 + m.m10*m31 + m.m20*m32 + m.m30*m33,
924  m.m01*m30 + m.m11*m31 + m.m21*m32 + m.m31*m33,
925  m.m02*m30 + m.m12*m31 + m.m22*m32 + m.m32*m33,
926  m.m03*m30 + m.m13*m31 + m.m23*m32 + m.m33*m33
927  );
928  }
929 
930  real determinant() const
931  {
932  //return m00*m11*m22*m33 - m00*m11*m23*m32 - m00*m12*m21*m33 + m00*m12*m23*m31 +
933  // m00*m13*m21*m32 - m00*m13*m22*m31 - m01*m10*m22*m33 + m01*m10*m23*m32 +
934  // m01*m12*m20*m33 - m01*m12*m23*m30 - m01*m13*m20*m32 + m01*m13*m22*m30 +
935  // m02*m10*m21*m33 - m02*m10*m23*m31 - m02*m11*m20*m33 + m02*m11*m23*m30 +
936  // m02*m13*m20*m31 - m02*m13*m21*m30 - m03*m10*m21*m32 + m03*m10*m22*m31 +
937  // m03*m11*m20*m32 - m03*m11*m22*m30 - m03*m12*m20*m31 + m03*m12*m21*m30;
938 
939  real x0 = m00*m11, x1 = m22*m33, x2 = m00*m12, x3 = m23*m31, x4 = m00*m13, x5 = m21*m32, x6 = m01*m10,
940  x7 = m23*m32, x8 = m01*m12, x9 = m20*m33, x10 = m01*m13, x11 = m22*m30, x12 = m02*m10, x13 = m21*m33,
941  x14 = m02*m11, x15 = m23*m30, x16 = m02*m13, x17 = m20*m31, x18 = m03*m10, x19 = m22*m31,
942  x20 = m03*m11, x21 = m20*m32, x22 = m03*m12, x23 = m21*m30;
943 
944  return x0*x1 - x0*x7 - x1*x6 + x10*x11 - x10*x21 - x11*x20 + x12*x13 - x12*x3 - x13*x2 + x14*x15 - x14*x9 - x15*x8 +
945  x16*x17 - x16*x23 - x17*x22 + x18*x19 - x18*x5 - x19*x4 + x2*x3 + x20*x21 + x22*x23 + x4*x5 + x6*x7 + x8*x9;
946  }
947 
948  mat4 inverse() const
949  {
950  real s0 = m00*m11 - m01*m10;
951  real s1 = m00*m12 - m02*m10;
952  real s2 = m00*m13 - m03*m10;
953  real s3 = m01*m12 - m02*m11;
954  real s4 = m01*m13 - m03*m11;
955  real s5 = m02*m13 - m03*m12;
956  real c5 = m22*m33 - m23*m32;
957  real c4 = m21*m33 - m23*m31;
958  real c3 = m21*m32 - m22*m31;
959  real c2 = m20*m33 - m23*m30;
960  real c1 = m20*m32 - m22*m30;
961  real c0 = m20*m31 - m21*m30;
962 
963  real invdet = 1.0 / (s0 * c5 - s1 * c4 + s2 * c3 + s3 * c2 - s4 * c1 + s5 * c0);
964 
965  real b00 = ( c3*m13 - c4*m12 + c5*m11 ) * invdet;
966  real b01 = ( -c3*m03 + c4*m02 - c5*m01 ) * invdet;
967  real b02 = ( m31*s5 - m32*s4 + m33*s3 ) * invdet;
968  real b03 = ( -m21*s5 + m22*s4 - m23*s3 ) * invdet;
969  real b10 = ( -c1*m13 + c2*m12 - c5*m10 ) * invdet;
970  real b11 = ( c1*m03 - c2*m02 + c5*m00 ) * invdet;
971  real b12 = ( -m30*s5 + m32*s2 - m33*s1 ) * invdet;
972  real b13 = ( m20*s5 - m22*s2 + m23*s1 ) * invdet;
973  real b20 = ( c0*m13 - c2*m11 + c4*m10 ) * invdet;
974  real b21 = ( -c0*m03 + c2*m01 - c4*m00 ) * invdet;
975  real b22 = ( m30*s4 - m31*s2 + m33*s0 ) * invdet;
976  real b23 = ( -m20*s4 + m21*s2 - m23*s0 ) * invdet;
977  real b30 = ( -c0*m12 + c1*m11 - c3*m10 ) * invdet;
978  real b31 = ( c0*m02 - c1*m01 + c3*m00 ) * invdet;
979  real b32 = ( -m30*s3 + m31*s1 - m32*s0 ) * invdet;
980  real b33 = ( m20*s3 - m21*s1 + m22*s0 ) * invdet;
981 
982  return mat4(b00,b01,b02,b03,b10,b11,b12,b13,b20,b21,b22,b23,b30,b31,b32,b33);
983  }
984 };
985 
993 class rotator
994 {
995 protected:
996  real _w,_x,_y,_z;
997 
998 public:
1000  rotator() : _w(1),_x(0),_y(0),_z(0)
1001  {}
1002 
1004  rotator(const rotator &r)
1005  {
1006  set(r);
1007  }
1008 
1010  rotator(const vec3& axis, real rads)
1011  {
1012  setAxis(axis,rads);
1013  }
1014 
1015 
1017  rotator(real x, real y, real z, real w)
1018  {
1019  set(x,y,z,w);
1020  }
1021 
1023  rotator(const vec3& from, const vec3& to)
1024  {
1025  if(from==to)
1026  set(0,0,0,1);
1027  //else if(from.isParallel(to)){
1028  // vec3 axis=from.isParallel(vec3(1,0,0)) ? vec3(0,1,0) : vec3(1,0,0);
1029  // setAxis(axis.cross(from),dPI);
1030  //}
1031  else
1032  setAxis(from.cross(to),from.angleTo(to));
1033  }
1034 
1036  rotator(real yaw, real pitch, real roll)
1037  {
1038  real c1 = cos(0.5*roll); // was yaw in the original (X=right, Y=up, Z=towards) axes definition
1039  real s1 = sin(0.5*roll);
1040  real c2 = cos(0.5*yaw); // was pitch
1041  real s2 = sin(0.5*yaw);
1042  real c3 = cos(0.5*pitch); // was roll
1043  real s3 = sin(0.5*pitch);
1044  real c1c2 = c1*c2;
1045  real s1s2 = s1*s2;
1046  real c1s2 = c1*s2;
1047  real s1c2 = s1*c2;
1048 
1049  _w =c1c2*c3 - s1s2*s3;
1050  _x =c1c2*s3 + s1s2*c3;
1051  _y =s1c2*c3 + c1s2*s3;
1052  _z =c1s2*c3 - s1c2*s3;
1053 
1054  // TODO: the above defines ordering of rotations as pitch-yaw-roll, is this correct? This instead perhaps:
1055  //set(rotator(vec3::Z(),yaw)*rotator(vec3::X(),pitch)*rotator(vec3::Y(),roll));
1056  }
1057 
1059  rotator(real m00,real m01,real m02,real m10,real m11,real m12,real m20,real m21,real m22)
1060  {
1061  real tr = m00 + m11 + m22;
1062 
1063  if (tr > 0) {
1064  real S = sqrt(tr+1.0) * 2; // S=4*qw
1065  _w = 0.25 * S;
1066  _x = (m21 - m12) / S;
1067  _y = (m02 - m20) / S;
1068  _z = (m10 - m01) / S;
1069  } else if(m00 > m11 && m00 > m22) {
1070  real S = sqrt(1.0 + m00 - m11 - m22) * 2; // S=4*qx
1071  _w = (m21 - m12) / S;
1072  _x = 0.25 * S;
1073  _y = (m01 + m10) / S;
1074  _z = (m02 + m20) / S;
1075  } else if (m11 > m22) {
1076  real S = sqrt(1.0 + m11 - m00 - m22) * 2; // S=4*qy
1077  _w = (m02 - m20) / S;
1078  _x = (m01 + m10) / S;
1079  _y = 0.25 * S;
1080  _z = (m12 + m21) / S;
1081  } else {
1082  real S = sqrt(1.0 + m22 - m00 - m11) * 2; // S=4*qz
1083  _w = (m10 - m01) / S;
1084  _x = (m02 + m20) / S;
1085  _y = (m12 + m21) / S;
1086  _z = 0.25 * S;
1087  }
1088  }
1089 
1096  rotator(vec3 row1, vec3 col1, vec3 row2, vec3 col2)
1097  {
1098  vec3 norm1=col1.cross(row1).norm(); // first plane normal
1099  vec3 norm2=col2.cross(row2).norm(); // second plane normal
1100  rotator rot;
1101 
1102  // define rot as the rotation aligning the second plane to the first
1103  if(norm1==-norm2)
1104  rot=rotator(row1,dPI); // flip along the row vector
1105  else
1106  rot=rotator(norm2,norm1);
1107 
1108  // combine rot with a rotation which aligns row vectors by rotating in the first plane, then assign values to this object
1109  //set(rotator(norm1,row1.angleTo(rot*row2))*rot); // y u no work?
1110  set(rotator(rot*row2,row1)*rot);
1111  }
1112 
1114  void setAxis(const vec3& axis, real rads)
1115  {
1116  if(!equalsEpsilon(rads,0.0) && !axis.isZero()){
1117  vec3 na=axis.norm();
1118  real srads = (real)sin(rads / 2.0);
1119  set(na.x()*srads,na.y()*srads,na.z()*srads,(real)cos(rads / 2.0));
1120  }
1121  else
1122  set(0,0,0,1);
1123  }
1124 
1126  void set(const rotator& r)
1127  {
1128  set(r._x,r._y,r._z,r._w);
1129  }
1130 
1132  void set(real ry,real rz, real rw)
1133  {
1134  set(sqrt(1.0-(ry*ry+rz*rz+rw*rw)),ry,rz,rw);
1135  }
1136 
1138  void set(real rx,real ry, real rz,real rw)
1139  {
1140  _x = rx;
1141  _y = ry;
1142  _z = rz;
1143  _w = rw;
1144  }
1145 
1146  real w() const { return _w; }
1147  real x() const { return _x; }
1148  real y() const { return _y; }
1149  real z() const { return _z; }
1150 
1152  real getPitch() const
1153  {
1154  real test=_x*_y+_z*_w;
1155  if(test>(0.5-dEPSILON))
1156  return 0;
1157 
1158  if(test<(-0.5+dEPSILON))
1159  return 0;
1160 
1161  return atan2(2*_x*_w-2*_y*_z , 1 - 2*_x*_x - 2*_z*_z);
1162  }
1163 
1165  real getYaw() const
1166  {
1167  real test=_x*_y+_z*_w;
1168  if(test>(0.5-dEPSILON))
1169  return dPI*0.5;
1170 
1171  if(test<(-0.5+dEPSILON))
1172  return dPI*-0.5;
1173 
1174  return asin(2*test);
1175  }
1176 
1178  real getRoll() const
1179  {
1180  real test=_x*_y+_z*_w;
1181  if(test>(0.5-dEPSILON))
1182  return 2*atan2(_x,_w);
1183 
1184  if(test<(-0.5+dEPSILON))
1185  return -2*atan2(_x,_w);
1186 
1187  return atan2(2*_y*_w-2*_x*_z , 1 - 2*_y*_y - 2*_z*_z);
1188  }
1189 
1191  vec3 operator * (const vec3 &v) const
1192  {
1193  vec3 axis(_x,_y,_z);
1194  vec3 vc=axis.cross(v);
1195  vec3 vcc=axis.cross(vc);
1196  return (vc*(2.0*_w))+(vcc*2.0)+v;
1197  }
1198 
1200  friend vec3 operator * (const vec3 &v,const rotator& r)
1201  {
1202  return r*v;
1203  }
1204 
1205  vec3 operator / (const vec3& v) const
1206  {
1207  return inverse()*v;
1208  }
1209 
1211  friend vec3 operator / (const vec3 &v,const rotator& r)
1212  {
1213  return r/v;
1214  }
1215 
1217  rotator operator * (const rotator & r) const
1218  {
1219  rotator rr;
1220  rr.set( _w * r._x + _x * r._w + _y * r._z - _z * r._y,
1221  _w * r._y + _y * r._w + _z * r._x - _x * r._z,
1222  _w * r._z + _z * r._w + _x * r._y - _y * r._x,
1223  _w * r._w - _x * r._x - _y * r._y - _z * r._z);
1224  return rr;
1225  }
1226 
1227  rotator operator * (real r) const
1228  {
1229  rotator rr;
1230  rr.set(_x*r,_y*r,_z*r,_w*r);
1231  return rr;
1232  }
1233 
1234  rotator operator + (const rotator & r) const
1235  {
1236  rotator rr;
1237  rr.set(_x+r._x,_y+r._y,_z+r._z,_w+r._w);
1238  return rr;
1239  }
1240 
1241  rotator operator - (const rotator & r) const
1242  {
1243  rotator rr;
1244  rr.set(_x-r._x,_y-r._y,_z-r._z,_w-r._w);
1245  return rr;
1246  }
1247 
1248  rotator operator - () const
1249  {
1250  rotator rr;
1251  rr.set(-_x,-_y,-_z,-_w);
1252  return rr;
1253  }
1254 
1255  bool operator == (const rotator & v) const
1256  {
1257  if(equalsEpsilon(_w,v._w) && equalsEpsilon(_x,v._x) && equalsEpsilon(_y,v._y) && equalsEpsilon(_z,v._z))
1258  return true;
1259 
1260  if(equalsEpsilon(-_w,v._w) && equalsEpsilon(-_x,v._x) && equalsEpsilon(-_y,v._y) && equalsEpsilon(-_z,v._z))
1261  return true;
1262 
1263  return false;
1264  }
1265 
1266  bool operator != (const rotator &v) const { return !((*this)==v); }
1267 
1269  {
1270  rotator rr;
1271  rr.set(-_x,-_y,-_z,_w);
1272  return rr;
1273  }
1274 
1275  real len() const
1276  {
1277  return sqrt(_x*_x+_y*_y+_z*_z+_w*_w);
1278  }
1279 
1280  real dot(const rotator &r) const
1281  {
1282  return _x*r._x + _y*r._y + _z*r._z + _w*r._w;
1283  }
1284 
1285  rotator norm() const
1286  {
1287  rotator rr(*this);
1288  rr.normThis();
1289  return rr;
1290  }
1291 
1292  void normThis()
1293  {
1294  real n=len();
1295  if(n!=0.0){
1296  _x/=n;
1297  _y/=n;
1298  _z/=n;
1299  _w/=n;
1300  }
1301  }
1302 
1305  {
1306  rotator rr=conjugate();
1307  rr.normThis();
1308  return rr;
1309  }
1310 
1312  rotator interpolate(real val,const rotator& r) const
1313  {
1314  if(val>=1)
1315  return r;
1316 
1317  if(val<=0)
1318  return *this;
1319 
1320  rotator rr;
1321  rr.set(*this + ((dot(r)<0.0 ? -r : r) - *this)*val);
1322  rr.normThis();
1323 
1324  return rr;
1325  }
1326 
1327  void toMatrix(real* mat) const
1328  {
1329  rotator r=this->norm();
1330  real x2=r.x()*r.x(),y2=r.y()*r.y(),z2=r.z()*r.z(),
1331  xy=r.x()*r.y(),xz=r.x()*r.z(),yz=r.y()*r.z(),
1332  wz=r.w()*r.z(),wx=r.w()*r.x(),wy=r.w()*r.y();
1333 
1334  mat[ 0] = 1.0f - 2.0f * ( y2 + z2 );
1335  mat[ 1] = 2.0f * (xy - wz);
1336  mat[ 2] = 2.0f * (xz + wy);
1337  mat[ 3] = 0.0f;
1338  mat[ 4] = 2.0f * (xy + wz);
1339  mat[ 5] = 1.0f - 2.0f * (x2 + z2);
1340  mat[ 6] = 2.0f * (yz - wx );
1341  mat[ 7] = 0.0f;
1342  mat[ 8] = 2.0f * (xz - wy);
1343  mat[ 9] = 2.0f * (yz + wx);
1344  mat[10] = 1.0f - 2.0f * (x2 + y2);
1345  mat[11] = 0.0f;
1346  mat[12] = 0;
1347  mat[13] = 0;
1348  mat[14] = 0;
1349  mat[15] = 1.0f;
1350  }
1351 
1352  mat4 toMatrix() const
1353  {
1354  mat4 m;
1355  toMatrix((real*)m.m);
1356  return m;
1357  }
1358 
1359  i32 hash() const
1360  {
1361  union { real f; i64 i;} x,y,z,w;
1362  x.f=_x;
1363  y.f=_y;
1364  z.f=_z;
1365  w.f=_w;
1366 
1367  i64 hash=_HASH(x.i,_HASH(y.i,_HASH(z.i,w.i,12),13),14);
1368  return i32(hash>>32)^i32(hash);
1369  }
1370 
1371  friend std::ostream& operator << (std::ostream &out,const rotator &r)
1372  {
1373  return out << "rotator(" << r.x() << ", " << r.y() << ", " << r.z() << ", " << r.w() << ")";
1374  }
1375 };
1376 
1377 /*****************************************************************************************************************************/
1378 /* Exceptions */
1379 /*****************************************************************************************************************************/
1380 
1381 class IndexException : public std::exception
1382 {
1383 protected:
1384  std::string name;
1385  std::string msg;
1386  size_t val, maxval;
1387 
1388  void setMsg()
1389  {
1390  std::ostringstream out;
1391  out << "Bad value " << val << " for index '" << name.c_str() << "' (0 <= " << name.c_str() << " < " << maxval << ")";
1392  msg=out.str();
1393  }
1394 
1395 public:
1396  IndexException(std::string name, size_t val, size_t maxval) : name(name),val(val),maxval(maxval)
1397  {
1398  setMsg();
1399  }
1400 
1401  IndexException(const IndexException& ind) : name(ind.name),val(ind.val),maxval(ind.maxval)
1402  {
1403  setMsg();
1404  }
1405 
1406  virtual ~IndexException() throw() {}
1407 
1408  virtual const char* what() const throw() { return msg.c_str(); }
1409 };
1410 
1411 class MemException : public std::exception
1412 {
1413 protected:
1414  std::string msg;
1415 public:
1416  MemException(const std::string msg) : msg(msg){}
1417 
1418  MemException(const std::string m,int err)
1419  {
1420  std::ostringstream out;
1421  out << m << ": errno: " << strerror(errno);
1422  msg=out.str();
1423  }
1424 
1425  MemException(const MemException & op) : msg(op.msg) {}
1426 
1427  virtual ~MemException() throw() {}
1428 
1429  virtual const char* what() const throw() { return msg.c_str(); }
1430 };
1431 
1432 class RenderException : public std::exception
1433 {
1434 protected:
1435  std::string msg;
1436 public:
1437  RenderException(const std::string msg) : msg(msg){}
1438 
1439  RenderException(const std::string msg, const char* file, int line){
1440  std::ostringstream out;
1441  out << file << ":" << line << ":" << msg;
1442  this->msg=out.str();
1443  }
1444 
1445  RenderException(const RenderException & op) : msg(op.msg) {}
1446 
1447  virtual ~RenderException() throw() {}
1448 
1449  virtual const char* what() const throw() { return msg.c_str(); }
1450 };
1451 
1452 class ValueException : public std::exception
1453 {
1454 protected:
1455  std::string msg;
1456 public:
1457  ValueException(const std::string valuename, const std::string msg, const char* file=NULL, int line=-1)
1458  {
1459  std::ostringstream out;
1460  if(file)
1461  out << file << ":" << line << ": ";
1462  out << "Bad value for " << valuename << ": " << msg;
1463  this->msg=out.str();
1464 
1465  }
1466 
1467  ValueException(const ValueException & op) : msg(op.msg) {}
1468 
1469  virtual ~ValueException() throw() {}
1470 
1471  virtual const char* what() const throw() { return msg.c_str(); }
1472 };
1473 
1474 inline void checkNull(const std::string valuename, const void* val, const char* file=NULL, int line=-1) throw(ValueException)
1475 {
1476  if(val==NULL)
1477  throw ValueException(valuename,"Must not be null",file,line);
1478 }
1479 
1480 #define CHECK_NULL(val) checkNull("val",(const void*)val,__FILE__,__LINE__)
1481 
1482 /*****************************************************************************************************************************/
1483 /* Data Structure Objects */
1484 /*****************************************************************************************************************************/
1485 
1487 void readBinaryFileToBuff(const char* filename,size_t offset,void* dest,size_t len) throw(MemException);
1488 
1490 void storeBufftoBinaryFile(const char* filename,void* src,size_t len,int* header, size_t headerlen) throw(MemException);
1491 
1494 {
1495 protected:
1496  //typedef std::pair<std::string,std::string> strpair;
1497  typedef std::map<std::string,std::string> metamap;
1498  typedef metamap::iterator iter;
1499  typedef metamap::const_iterator citer;
1500 
1501  metamap _meta;
1502 public:
1503 
1505  bool hasMetaKey(const char* key) const { return _meta.find(key)!=_meta.end(); }
1506 
1507  std::vector<std::string> getMetaKeys() const
1508  {
1509  std::vector<std::string> result;
1510  for(citer i=_meta.begin();i!=_meta.end();i++)
1511  result.push_back((*i).first);
1512 
1513  return result;
1514  }
1515 
1516  std::string meta() const
1517  {
1518  std::ostringstream out;
1519  for(citer c=_meta.begin();c!=_meta.end();c++)
1520  out << (*c).first << " = " << (*c).second << std::endl;
1521 
1522  return out.str();
1523  }
1524 
1526  const char* meta(const char* key) const
1527  {
1528  return hasMetaKey(key) ? _meta.find(key)->second.c_str() : "";
1529  }
1530 
1532  void meta(const char* key, const char* val)
1533  {
1534  if(!key || !val)
1535  return;
1536 
1537  std::string fkey=key,fval=val;
1538 
1539  // filter out | characters
1540  fkey.erase (std::remove(fkey.begin(), fkey.end(), '|'), fkey.end());
1541  fval.erase (std::remove(fval.begin(), fval.end(), '|'), fval.end());
1542 
1543  _meta[fkey]=fval;
1544  }
1545 
1547  void meta(const MetaType* m)
1548  {
1549  for(citer i=m->_meta.begin();i!=m->_meta.end();i++)
1550  meta((*i).first.c_str(),(*i).second.c_str());
1551  }
1552 
1554  std::string serializeMeta() const
1555  {
1556  std::ostringstream out;
1557  for(citer i=_meta.begin();i!=_meta.end();i++)
1558  out << (*i).first << "||" << (*i).second << "||";
1559 
1560  return out.str();
1561  }
1562 
1564  void deserializeMeta(const std::string &s)
1565  {
1566  char *buf=new char[s.length()+1];
1567  strcpy(buf,s.c_str());
1568 
1569  char* p=strtok(buf,"|");
1570 
1571  while(p!=NULL){
1572  std::string key=p;
1573  p=strtok(NULL,"|");
1574  std::string val=p;
1575  _meta[key]=val;
1576  p=strtok(NULL,"|");
1577  }
1578 
1579  delete buf;
1580  }
1581 };
1582 
1583 // Metaprogramming types encapsulating the 4 operators +-/*
1584 template<typename R,typename LH, typename RH> struct AddOp { static inline R op(const LH& lh, const RH& rh) { return lh+rh; } };
1585 template<typename R,typename LH, typename RH> struct SubOp { static inline R op(const LH& lh, const RH& rh) { return lh-rh; } };
1586 template<typename R,typename LH, typename RH> struct DivOp { static inline R op(const LH& lh, const RH& rh) { return lh/rh; } };
1587 template<typename R,typename LH, typename RH> struct MulOp { static inline R op(const LH& lh, const RH& rh) { return lh*rh; } };
1588 
1589 
1590 // Metaprogramming types for encapsulating the endian swap functions
1591 template<typename T> struct SwapEndian { static T swap(T t) { return swapEndianN(t); } };
1592 template<> struct SwapEndian<real> { static real swap(real t) { return swapEndian64(t); } };
1593 template<> struct SwapEndian<indexval> { static indexval swap(indexval t) { return swapEndian32(t); } };
1594 template<> struct SwapEndian<vec3> { static vec3 swap(vec3 t) { return vec3(swapEndian64(t.x()),swapEndian64(t.y()),swapEndian64(t.z())); } };
1595 template<> struct SwapEndian<color> { static color swap(color t) { return color(swapEndian32(t.r()),swapEndian32(t.g()),swapEndian32(t.b()),swapEndian32(t.a())); } };
1596 
1606 template<typename T> class Matrix : public MetaType
1607 {
1608 protected:
1609  std::string _name; // matrix name, should be unique application-wide
1610  std::string _type; // type of matrix, may be an empty string if types don't make sense for what's stored
1611  std::string _sharedname; // name of the share memory segment `data' refers to
1612 
1613  T* data; // refers to locally-allocated segments of mmap-addressed shared memory segments
1614  sval _n_actual; // actual length of allocated data, >=_n
1615  sval _n,_m; // _n rows, _m columns
1616 
1617  bool _isShared; // true if the memory is shared, false if locally allocated memory
1618 
1619 #ifdef WIN32
1620  HANDLE mapFile;
1621 #endif
1622 
1623 public:
1624 
1626  Matrix(const char* name,sval n, sval m=1,bool isShared=false) throw(MemException) :
1627  _name(name), _type(""),_sharedname(""),data(0),_n_actual(0),_n(n),_m(m),_isShared(false)
1628  {
1629  checkDimension("m",m);
1630  setShared(isShared);
1631  }
1632 
1634  Matrix(const char* name,const char* type,sval n, sval m=1,bool isShared=false) throw(MemException) :
1635  _name(name), _type(type),_sharedname(""),data(0),_n_actual(0),_n(n),_m(m),_isShared(false)
1636  {
1637  checkDimension("m",m);
1638  setShared(isShared);
1639  }
1640 
1642  Matrix(const char* name,const char* type,const char* sharedname,const char* serialmeta,sval n, sval m) throw(MemException) :
1643  _name(name), _type(type),_sharedname(sharedname),data(0),_n_actual(n),_n(n),_m(m),_isShared(true)
1644  {
1645  checkDimension("n",n);
1646  checkDimension("m",m);
1647  deserializeMeta(serialmeta);
1648  data=createShared();
1649  }
1650 
1652  Matrix(const char* name,const char* type,const T* array,sval n, sval m,bool isShared=false) throw(MemException) :
1653  _name(name), _type(type),_sharedname(""),data(0),_n_actual(0),_n(n),_m(m),_isShared(false)
1654  {
1655  checkDimension("n",n);
1656  checkDimension("m",m);
1657  setShared(isShared);
1658  memcpy(data,array,memSize());
1659  }
1660 
1661  virtual ~Matrix()
1662  {
1663  clear();
1664  }
1665 
1666  T* dataPtr() const { return data; }
1667 
1669  Matrix<T>* clone(const char* newname=NULL, bool isShared=false) const
1670  {
1671  Matrix<T>* m=new Matrix<T>(newname ? newname : _name.c_str(),_type.c_str(),_n,_m,isShared);
1672  memcpy(m->data,data,memSize());
1673  m->meta(this);
1674  return m;
1675  }
1676 
1677  const char* getName() const { return _name.c_str(); }
1678  const char* getSharedName() const { return _sharedname.c_str(); }
1679  const char* getType() const { return _type.c_str(); }
1680 
1681  void setName(const char* name) { _name=name; }
1682  void setType(const char* type) { _type=type; }
1683 
1685  bool isShared() const { return _isShared; }
1686 
1695  void setShared(bool val) throw(MemException)
1696  {
1697  if(data && val==_isShared) // do nothing if the shared state to set is the current state and the matrix is allocated
1698  return;
1699 
1700  if(val){
1701  _n_actual=_n;
1702  sval size=memSize();
1703 
1704  if(size==0)
1705  throw MemException("Cannot make empty matrix shared");
1706 
1707  T* shared=createShared();
1708 
1709  if(data){
1710  memcpy(shared,data,size);
1711  delete[] data;
1712  }
1713  else
1714  memset(shared,0,size);
1715 
1716  data=shared;
1717  }
1718  else{
1719  T* olddata=data;
1720  resize();
1721  closeShared(olddata);
1722  }
1723 
1724  _isShared=val;
1725  }
1726 
1727  void clear() throw(MemException)
1728  {
1729  if(data){
1730  if(_isShared){
1731  closeShared(data);
1732  unlinkShared(_sharedname);
1733  }
1734  else if(data!=NULL)
1735  delete[] data;
1736  }
1737 
1738  _n_actual=0;
1739  _n=0;
1740  _isShared=false;
1741  data=NULL;
1742  }
1743 
1745  sval n() const { return _n; }
1746 
1748  sval m() const { return _m; }
1749 
1751  sval memSize() const { return sval(sizeof(T)*_n*_m); }
1752 
1754  void fill(const T& t)
1755  {
1756  T* d=data;
1757  for(sval n=0;n<_n*_m;n++)
1758  *d++=t;
1759  }
1760 
1762  template<typename R> void copyFrom(const Matrix<R>* r)
1763  {
1764  sval minsize=_min(memSize(),r->memSize());
1765  if(minsize>0)
1766  memcpy(data,r->dataPtr(),minsize);
1767  }
1768 
1770  Matrix<T>* subMatrix(const char* name,sval n, sval m=1,sval noff=0,sval moff=0,bool isShared=false) const throw(MemException)
1771  {
1772  checkDimension("n",n);
1773  checkDimension("m",m);
1774 
1775  if(n>_n || m>_m)
1776  throw MemException("Submatrix dimensions may not exceed matrix dimensions");
1777 
1778  if((n+noff)>_n || (m+moff)>_m)
1779  throw MemException("Submatrix dimensions plus offsets may not exceed matrix dimensions");
1780 
1781  Matrix<T>* mat=new Matrix<T>(name,_type.c_str(),n,m,isShared);
1782 
1783  for(sval nn=0;nn<n;nn++)
1784  for(sval mm=0;mm<m;mm++)
1785  mat->at(nn,mm)=at(nn+noff,mm+moff);
1786 
1787  return mat;
1788  }
1789 
1795  Matrix<T>* reshape(const char* name,sval n, sval m,bool isShared=false) const throw(MemException)
1796  {
1797  checkDimension("n",n);
1798  checkDimension("m",m);
1799 
1800  Matrix<T>* mat=new Matrix<T>(name,_type.c_str(),n,m,isShared);
1801  memcpy(mat->dataPtr(),data,_min(mat->memSize(),memSize()));
1802  return mat;
1803  }
1804 
1805  // These methods define mathematical operators for single value or matrix right hand sides
1806 
1808  template<typename Ctx>
1809  void applyFunc(T (*op)(Ctx,const T&,sval,sval),Ctx ctx,sval minrow=0,sval mincol=0,sval maxrow=-1,sval maxcol=-1)
1810  {
1811  maxcol=_min(_m,maxcol);
1812  maxrow=_min(_n,maxrow);
1813 
1814  for(sval n=minrow;n<maxrow;n++)
1815  for(sval m=mincol;m<maxcol;m++)
1816  at(n,m)=op(ctx,at(n,m),n,m);
1817  }
1818 
1820  template<typename R, typename OpType>
1821  void scalarop(const R& r,sval minrow=0,sval mincol=0,sval maxrow=-1,sval maxcol=-1)
1822  {
1823  maxcol=_min(_m,maxcol);
1824  maxrow=_min(_n,maxrow);
1825 
1826  for(sval n=minrow;n<maxrow;n++)
1827  for(sval m=mincol;m<maxcol;m++)
1828  at(n,m)=OpType::op(at(n,m),r);
1829  }
1830 
1832  template<typename R, typename OpType>
1833  void matop(const Matrix<R>& mat,sval minrow=0,sval mincol=0,sval maxrow=-1,sval maxcol=-1)
1834  {
1835  maxcol=_min(_min(mat.m(),_m),maxcol);
1836  maxrow=_min(_min(mat.n(),_n),maxrow);
1837 
1838  for(sval n=minrow;n<maxrow;n++)
1839  for(sval m=mincol;m<maxcol;m++)
1840  at(n,m)=OpType::op(at(n,m),mat(n,m));
1841  }
1842 
1844  template<typename R> void add(const R& r,sval minrow=0,sval mincol=0,sval maxrow=-1,sval maxcol=-1)
1845  {
1846  return scalarop<R,AddOp<T,T,R> >(r,minrow,mincol,maxrow,maxcol);
1847  }
1848 
1850  template<typename R> void sub(const R& r,sval minrow=0,sval mincol=0,sval maxrow=-1,sval maxcol=-1)
1851  {
1852  return scalarop<R,SubOp<T,T,R> >(r,minrow,mincol,maxrow,maxcol);
1853  }
1854 
1856  template<typename R> void mul(const R& r,sval minrow=0,sval mincol=0,sval maxrow=-1,sval maxcol=-1)
1857  {
1858  return scalarop<R,MulOp<T,T,R> >(r,minrow,mincol,maxrow,maxcol);
1859  }
1860 
1862  template<typename R> void div(const R& r,sval minrow=0,sval mincol=0,sval maxrow=-1,sval maxcol=-1)
1863  {
1864  return scalarop<R,DivOp<T,T,R> >(r,minrow,mincol,maxrow,maxcol);
1865  }
1866 
1868  template<typename R> void addm(const Matrix<R>& mat,sval minrow=0,sval mincol=0,sval maxrow=-1,sval maxcol=-1)
1869  {
1870  return matop<R,AddOp<T,T,R> >(mat,minrow,mincol,maxrow,maxcol);
1871  }
1872 
1874  template<typename R> void subm(const Matrix<R>& mat,sval minrow=0,sval mincol=0,sval maxrow=-1,sval maxcol=-1)
1875  {
1876  return matop<R,SubOp<T,T,R> >(mat,minrow,mincol,maxrow,maxcol);
1877  }
1878 
1880  template<typename R> void mulm(const Matrix<R>& mat,sval minrow=0,sval mincol=0,sval maxrow=-1,sval maxcol=-1)
1881  {
1882  return matop<R,MulOp<T,T,R> >(mat,minrow,mincol,maxrow,maxcol);
1883  }
1884 
1886  template<typename R> void divm(const Matrix<R>& mat,sval minrow=0,sval mincol=0,sval maxrow=-1,sval maxcol=-1)
1887  {
1888  return matop<R,DivOp<T,T,R> >(mat,minrow,mincol,maxrow,maxcol);
1889  }
1890 
1891  void reorderColumns(const sval *orderinds) throw(IndexException)
1892  {
1893  T* buff=new T[_m];
1894 
1895  for(sval j=0;j<_m;j++)
1896  checkIndex("sortinds",orderinds[j],_m);
1897 
1898  for(sval i=0;i<_n;i++){
1899  for(sval j=0;j<_m;j++)
1900  buff[j]=at(i,j);
1901 
1902  for(sval j=0;j<_m;j++)
1903  at(i,j)=buff[orderinds[j]];
1904  }
1905 
1906  delete buff;
1907  }
1908 
1909  void swapEndian()
1910  {
1911  sval len=_n*_m;
1912  for(sval i=0;i<len;i++)
1913  data[i]=SwapEndian<T>::swap(data[i]);
1914  }
1915 
1917  T& at(sval n, sval m=0) const { return data[m+(_m*n)]; }
1918  const T& atc(sval n, sval m=0) const { return data[m+(_m*n)]; }
1919  void ats(sval n, sval m, const T& t) { data[m+(_m*n)]=t; }
1920 
1922  T& operator () (sval n, sval m=0) const { return data[m+(_m*n)]; }
1923 
1924  T& operator [] (sval n) const { return data[n]; }
1925 
1927  T getAt(sval n, sval m=0) const throw(IndexException) { return data[getIndex(n,m)]; }
1928 
1930  void setAt(const T& t, sval n, sval m=0) throw(IndexException) { data[getIndex(n,m)]=t; }
1931 
1933  void setN(sval _newn) throw(MemException)
1934  {
1935  checkNotShared();
1936  _n=_newn;
1937  resize();
1938  }
1939 
1941  void setM(sval _newm) throw(MemException)
1942  {
1943  checkDimension("_newm",_newm);
1944  checkNotShared();
1945  _newm=_max<sval>(1,_newm);
1946  if(_newm>(_m*_n))
1947  throw MemException("New m value larger than matrix size");
1948 
1949  _n=sval((_n*_m)/_newm);
1950  _m=_newm;
1951  }
1952 
1954  void addRows(sval num) throw(MemException) { setN(_n+num); }
1955 
1957  void reserveRows(sval num) throw(MemException) { checkNotShared(); resize(num); }
1958 
1960  void append(const Matrix<T> &t) throw(MemException)
1961  {
1962  if(t.m()!=_m)
1963  throw MemException("Column dimensions of `this' and `t' do not match");
1964 
1965  sval oldn=_n;
1966  addRows(t.n());
1967  memcpy(data+(_m*oldn),t.data,t.memSize());
1968  }
1969 
1971  void append(const T& t,sval m=0) throw(MemException)
1972  {
1973  addRows(1);
1974  at(_n-1,m)=t;
1975  }
1976 
1978  {
1979  checkNotShared();
1980  checkIndex("n",n,_n);
1981 
1982  if(n<(_n-1))
1983  std::memmove(&data[n*_m],&data[(n+1)*_m],(_n-n-1)*_m*sizeof(T));
1984 
1985  setN(_n-1);
1986  }
1987 
1989  void readBinaryFile(const char* filename,size_t offset) throw(MemException)
1990  {
1991  readBinaryFileToBuff(filename,offset,data,memSize());
1992  }
1993 
1995  void readTextFile(const char* filename,sval numHeaders) throw(MemException)
1996  {
1997  setN(0);
1998  readTextFileMatrix(filename,numHeaders,this);
1999  }
2000 
2002  void storeBinaryFile(const char* filename, int* header, sval headerlen) throw(MemException)
2003  {
2004  storeBufftoBinaryFile(filename,data,memSize(),header,headerlen);
2005  }
2006 
2008  indexpair indexOf(const T& t,sval aftern=0,sval afterm=0) const
2009  {
2010  sval numelems=_n*_m;
2011  sval nm=afterm+aftern*_m;
2012 
2013  while(nm<numelems && data[nm]!=t)
2014  nm++;
2015 
2016  return indexpair(nm/_m,nm%_m);
2017  }
2018 
2019 protected:
2020 
2022  inline sval getIndex(sval n, sval m) const throw(IndexException)
2023  {
2024  checkIndex("n",n,_n);
2025  checkIndex("m",m,_m);
2026 
2027  return m+(_m*n);
2028  }
2029 
2030  inline void checkIndex(const char* name, sval val, sval maxval) const throw(IndexException)
2031  {
2032  if(val>=maxval)
2033  throw IndexException(name,val,maxval);
2034  }
2035 
2036  inline void checkNotShared() const throw(MemException)
2037  {
2038  if(_isShared)
2039  throw MemException("Operation may only be performed on non-shared matrices");
2040  }
2041 
2042  inline void checkDimension(const char* name, sval dim) const throw(MemException)
2043  {
2044  if(dim==0){
2045  std::ostringstream out;
2046  out << "Matrix dimension " << name << " must be non-zero";
2047  throw MemException(out.str());
2048  }
2049  }
2050 
2058  void resize(sval reserveNum=0)
2059  {
2060  if((_n+reserveNum)<=_n_actual && !_isShared) // do nothing if we have enough rows to meet demand and we're not shared
2061  return;
2062 
2063  // arbitrary min allocation of 1000 rows and 50% reserve, or just _n if this is the first allocation
2064  sval new_n_actual=data==NULL ? _n : _max<sval>(1000ul,((_n*3)/2)+reserveNum);
2065 
2066  if(new_n_actual<_n_actual && !_isShared)
2067  return;
2068 
2069  T* olddata=data;
2070  data=new T[new_n_actual*_m];
2071 
2072  memset(data,0,new_n_actual*_m*sizeof(T)); // always do this to ensure rows past _n_actual are clean
2073 
2074  if(olddata){
2075  memcpy(data,olddata,_n_actual*_m*sizeof(T));
2076  if(!_isShared)
2077  delete[] olddata;
2078  }
2079 
2080  _n_actual=new_n_actual;
2081  }
2082 
2084  void chooseSharedName(int counter=0)
2085  {
2086  std::ostringstream out;
2087 #ifdef WIN32
2088  out << "Local\\" ;
2089 
2090  if(counter>0)
2091  out << std::hex << counter << std::dec;
2092 
2093  out << GetCurrentProcessId() << _name;
2094  _sharedname=out.str();
2095 #else
2096 
2097 // OSX shm_open requires names to be shorter than normal
2098 #ifdef __APPLE__
2099  if(counter>0)
2100  out << std::hex << counter << std::dec;
2101 
2102  out << getpid() << _name;
2103 #else
2104  out << "__viz__" << getppid() << "_" << getpid();
2105  if(counter>0)
2106  out << "_" << std::hex << counter << std::dec;
2107 
2108  out << "_" << _name;
2109 #endif // __APPLE__
2110 
2111  _sharedname=out.str();
2112 
2113  for(sval i=0;i<_sharedname.size();i++)
2114  if(_sharedname[i]=='/')
2115  _sharedname[i]='_';
2116 
2117  if(_sharedname.size()>=MAXSHMNAMLEN)
2118  _sharedname[MAXSHMNAMLEN-1]='\0';
2119 #endif // WIN32
2120  }
2121 
2124  {
2125  sval size=memSize();
2126  T* ptr;
2127  bool isCreator=_sharedname=="";
2128 
2129  if(isCreator)
2130  chooseSharedName(); // start with the default shared name
2131 
2132 #ifdef WIN32
2133  std::ostringstream out;
2134 
2135  createFileMapping();
2136 
2137  // attempt to choose a unique shared name only when creating a segment
2138  for(int c=1;c<100000 && isCreator && mapFile && GetLastError()==ERROR_ALREADY_EXISTS;c++){
2139  CloseHandle(mapFile);
2140  chooseSharedName(c);
2141  createFileMapping();
2142  }
2143 
2144  if(!mapFile){
2145  out << "Unable to open shared memory handle to " << _sharedname << ": " << formatLastErrorMsg();
2146  throw MemException(out.str());
2147  }
2148 
2149  ptr=(T*)MapViewOfFile(mapFile,FILE_MAP_ALL_ACCESS,0,0,size);
2150 
2151  if(!ptr && isCreator && GetLastError()==ERROR_ACCESS_DENIED){
2152  for(int c=1;c<100000 && mapFile && !ptr && (GetLastError()==ERROR_ACCESS_DENIED || GetLastError()==ERROR_ALREADY_EXISTS);c++){
2153  CloseHandle(mapFile);
2154  chooseSharedName(c);
2155  createFileMapping();
2156 
2157  if(mapFile)
2158  ptr=(T*)MapViewOfFile(mapFile,FILE_MAP_ALL_ACCESS,0,0,size);
2159  }
2160  }
2161 
2162  if(!ptr){
2163  CloseHandle(mapFile);
2164  out << "Unable to map view of memory file " << _sharedname << ": " << formatLastErrorMsg();
2165  throw MemException(out.str());
2166  }
2167 
2168 #else // Linux/OSX
2169 
2170  int shm_fd=shm_open(_sharedname.c_str(), O_CREAT | O_RDWR | (isCreator ? O_EXCL : 0), S_IRUSR | S_IWUSR);
2171 
2172  // attempt to choose a unique shared name only when creating a segment
2173  for(int c=1;c<100000 && isCreator && shm_fd==-1 && errno==EEXIST;c++){
2174  chooseSharedName(c);
2175  shm_fd=shm_open(_sharedname.c_str(), O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
2176  }
2177 
2178  if(shm_fd==-1)
2179  throw MemException(std::string("Unable to open shared memory descriptor, filename:")+_sharedname,errno);
2180 
2181  if(isCreator && ftruncate(shm_fd, size) == -1)
2182  throw MemException(std::string("Unable to extend shared memory section, filename:")+_sharedname,errno);
2183 
2184  ptr = (T*)mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
2185 
2186  if(!ptr || ptr==MAP_FAILED){
2187  if(isCreator)
2188  shm_unlink(_sharedname.c_str());
2189 
2190  throw MemException("Unable to mmap shared memory",errno);
2191  }
2192 
2193  addShared(_sharedname); // store the name for later cleanup
2194 
2195  close(shm_fd);
2196 #endif // Linux/OSX
2197 
2198  return ptr;
2199  }
2200 
2202  void closeShared(T* ptr) throw(MemException)
2203  {
2204  if(!ptr)
2205  return;
2206 
2207 #ifdef WIN32
2208  if(!UnmapViewOfFile(ptr))
2209  throw MemException("Failed to unmap file view");
2210 
2211  if(!CloseHandle(mapFile))
2212  throw MemException("Failed to close handle");
2213 #else
2214  int mures=munmap(ptr,memSize());
2215  if(mures==-1)
2216  throw MemException("Failed to unmap memory section");
2217 #endif // WIN32
2218  _sharedname="";
2219  }
2220 
2221 #ifdef WIN32
2222  void createFileMapping()
2223  {
2224 #ifdef UNICODE
2225  const char* name=_sharedname.c_str();
2226  wchar_t namebuff[1024];
2227 
2228  ::MultiByteToWideChar(CP_ACP, NULL,name, -1, namebuff,int(_sharedname.size()+1));
2229 #else
2230  const char* namebuff=_sharedname.c_str();
2231 #endif // UNICODE
2232 
2233  mapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, DWORD(memSize()), namebuff);
2234  }
2235 #endif // WIN32
2236 };
2237 
2242 
2252 class DataSet : public MetaType
2253 {
2254 protected:
2255  std::string name;
2256  std::vector<std::string> indexnames;
2257  std::vector<std::string> fieldnames;
2258 
2259 public:
2260  DataSet(const char* name): name(name) { }
2261 
2262  const char* getName() const { return name.c_str(); }
2263 
2264  virtual DataSet* clone(const char* name,bool cloneNodes=false){ return NULL;}
2265 
2266  virtual Vec3Matrix* getNodes() const { return NULL ;}
2267  virtual void setNodes(Vec3Matrix *nodes) { }
2268 
2270  virtual std::vector<std::string> getIndexNames() const { return indexnames; }
2271 
2273  virtual void setIndexNames(std::vector<std::string> names) { indexnames=names; }
2274 
2276  virtual IndexMatrix* getIndexSet(const char* name) const { return NULL; }
2277 
2279  virtual bool hasIndexSet(const char* name) const { return getIndexSet(name)!=NULL; }
2280 
2282  virtual void setIndexSet(IndexMatrix *indices,const char *alias=NULL) {}
2283 
2285  virtual std::vector<std::string> getFieldNames() const { return fieldnames; }
2286 
2288  virtual void setFieldNames(std::vector<std::string> names) { fieldnames=names; }
2289 
2291  virtual RealMatrix* getDataField(const char* name) const { return NULL; }
2292 
2294  virtual bool hasDataField(const char* name) const { return getDataField(name)!=NULL; }
2295 
2297  virtual void setDataField(RealMatrix *field,const char *alias=NULL) {}
2298 };
2299 
2300 /*****************************************************************************************************************************/
2301 /* Scene Objects */
2302 /*****************************************************************************************************************************/
2303 
2304 class Material;
2305 
2307 class Texture
2308 {
2309 public:
2311  virtual ~Texture() {}
2312 
2313  virtual const char* getName() const {return "";}
2314  virtual const char* getFilename() const {return "";}
2315  virtual sval getWidth() const { return 0;}
2316  virtual sval getHeight() const { return 0;}
2317  virtual sval getDepth() const { return 0; }
2318  virtual bool hasAlpha() const {return false;}
2319  virtual TextureFormat getFormat() const { return TF_UNKNOWN; }
2320  virtual void fillBlack() {}
2321  virtual void fillColor(color col) {}
2322  virtual void fillColor(const ColorMatrix *mat,indexval depth) {}
2323  virtual void fillColor(const RealMatrix *mat,indexval depth,real minval=0.0,real maxval=1.0, const Material* colormat=NULL,const RealMatrix *alphamat=NULL,bool mulAlpha=true) {}
2324 };
2325 
2328 {
2329 public:
2330  virtual ~GPUProgram(){}
2331 
2332  virtual std::string getName() const {return "";}
2333  virtual void setType(ProgramType pt) {}
2334  virtual ProgramType getType() const { return PT_VERTEX; }
2335  virtual std::string getLanguage() const { return ""; }
2336 
2338  virtual void setLanguage(const std::string& lang) {}
2339 
2341  virtual void setSourceCode(const std::string& code){}
2342 
2344  virtual bool hasError() const { return false; }
2345 
2347  virtual std::string getSourceCode() const { return ""; }
2348 
2349  virtual bool setParameter(const std::string& param, const std::string& val) { return false; }
2350  virtual std::string getParameter(const std::string& param) const { return ""; }
2351  virtual std::string getEntryPoint() const { return getParameter("entry_point"); }
2352  virtual std::string getProfiles() const { return getParameter("profiles"); }
2353  virtual std::vector<std::string> getParameterNames() const { return std::vector<std::string>(); }
2354  virtual void setEntryPoint(const std::string main) { setParameter("entry_point",main); }
2355  virtual void setProfiles(const std::string profiles) { setParameter("profiles",profiles); }
2356 };
2357 
2358 template<typename T>
2360 {
2361 protected:
2362  typedef std::pair<real,T> ListValue;
2364 
2365 public:
2366  PositionQueue() : vals("vals",0) {}
2367  virtual ~PositionQueue() {}
2368 
2369  void copyFrom(const PositionQueue<T>* queue)
2370  {
2371  clear();
2372  vals.append(queue->vals);
2373  }
2374 
2375  void add(real pos,T val)
2376  {
2377  vals.append(ListValue(pos,val));
2378  sort();
2379  }
2380 
2381  void fill(const RealMatrix* pos, const Matrix<T>* ctrls)
2382  {
2383  vals.setN(pos->n());
2384  for(indexval i=0;i<pos->n();i++)
2385  vals[i]=ListValue(pos->at(i),ctrls->at(i));
2386  }
2387 
2388  sval size() const { return vals.n();}
2389 
2390  void clear() { vals.setN(0); }
2391 
2392  T get(indexval index) const throw(IndexException)
2393  {
2394  return vals.getAt(index).second;
2395  }
2396 
2397  void set(indexval index,real pos, T value) throw(IndexException)
2398  {
2399  vals.setAt(ListValue(pos,value),index);
2400  sort();
2401  }
2402 
2403  real pos(indexval index) const throw(IndexException)
2404  {
2405  return vals.getAt(index).first;
2406  }
2407 
2408  void remove(indexval index) throw(IndexException)
2409  {
2410  vals.removeRow(index);
2411  sort();
2412  }
2413 
2414  indexval find(real pos, const T& value) const
2415  {
2416  indexval i=0;
2417  sval numVals=vals.n();
2418 
2419  while(i<numVals && (!equalsEpsilon(vals[i].first,pos) || vals[i].second!=value))
2420  i++;
2421 
2422  return i;
2423  }
2424 
2425  void sort()
2426  {
2427  if(vals.n()>1)
2428  qsort(vals.dataPtr(),vals.n(),sizeof(ListValue),sortTupleFirstCB<ListValue>);
2429  }
2430 };
2431 
2437 template<typename T>
2439 {
2440 protected:
2443 
2444 public:
2445  ControlCurve() : ctrls("ctrls","",0,1), derivs("derivs","",0,2) {}
2446  virtual ~ControlCurve() {}
2447 
2448  void copyFrom(const ControlCurve<T> *con)
2449  {
2450  ctrls.setN(0);
2451  ctrls.append(con->ctrls);
2452  calculateDerivs();
2453  }
2454 
2455  void clear()
2456  {
2457  ctrls.setN(0);
2458  derivs.setN(0);
2459  }
2460 
2461  virtual void addCtrlPoint(const T& t) { ctrls.append(t); calculateDerivs(); }
2462  virtual void setCtrlPoint(const T& t,indexval index) throw(IndexException) { ctrls.setAt(t,index); calculateDerivs(); }
2463  virtual void removeCtrlPoint(indexval index) throw(IndexException) { ctrls.removeRow(index); calculateDerivs(); }
2464  virtual sval numPoints() const { return ctrls.n(); }
2465  virtual T getCtrlPoint(indexval index) const throw(IndexException) { return ctrls.getAt(index); }
2466 
2467  virtual void setCtrlPoints(const Matrix<T>* pts)
2468  {
2469  ctrls.setN(0);
2470  for(sval i=0;i<pts->n();i++)
2471  ctrls.append(pts->at(i));
2472 
2473  calculateDerivs();
2474  }
2475 
2476  virtual void calculateDerivs()
2477  {
2478  sval n=ctrls.n();
2479  derivs.setN(n);
2480 
2481  if(n==1){
2482  derivs(0,0)=ctrls[0];
2483  derivs(0,1)=ctrls[0];
2484  }
2485  else if(n==2){
2486  derivs(0,0)=ctrls[0];
2487  derivs(0,1)=ctrls[1];
2488  derivs(1,0)=ctrls[1];
2489  derivs(1,1)=ctrls[0];
2490  }
2491  else if(n>2){
2492  RealMatrix mat("",n,3);
2493 
2494  Matrix<T> localderivs("localderivs",n,1);
2495 
2496  localderivs[0]=ctrls[0];
2497  localderivs[n-1]=ctrls[n-1];
2498 
2499  if(n==3)
2500  localderivs[1]=ctrls[1]*6-ctrls[0]-ctrls[n-1];
2501  else{
2502  localderivs[1]=ctrls[1]*6-ctrls[0];
2503  localderivs[n-2]=ctrls[n-2]*6-ctrls[n-1];
2504 
2505  for(indexval i=2;i<n-2;i++)
2506  localderivs[i]=ctrls[i]*6;
2507  }
2508 
2509  for(indexval i=0;i<n;i++){
2510  mat(i,0)=4.0;
2511  mat(i,1)=1.0;
2512  mat(i,2)=1.0;
2513  }
2514 
2515  // Gaussian elimination
2516  for(indexval i=2;i<n-1;i++){
2517  mat(i,1)=mat(i,1)/mat(i-1,0);
2518  mat(i,0)=mat(i,0)-(mat(i,1)*mat(i-1,2));
2519  localderivs[i]=localderivs[i]-(localderivs[i-1]*mat(i,1));
2520  }
2521 
2522  localderivs[n-2]=localderivs[n-2]/mat(n-2,0);
2523 
2524  for(indexval i=n-3;i>0;i--)
2525  localderivs[i]=(localderivs[i]-(localderivs[i+1]*mat(i,2)))/mat(i,0);
2526 
2527  for(indexval s=0;s<n;s++){
2528  indexval e=clamp<indexval>(s+1,0,n-1);
2529  derivs(s,0)=(localderivs[s] * (2.0 / 3.0)) + (localderivs[e] / 3.0);
2530  derivs(s,1)=(localderivs[s] / 3.0) + (localderivs[e] * (2.0 / 3.0));
2531  }
2532  }
2533  // else n==0 do nothing
2534  }
2535 
2536  virtual T at(real tt) const
2537  {
2538  indexval n=ctrls.n();
2539  real tn=tt*(n-1);
2540 
2541  indexval s=clamp<indexval>(indexval(tn),0,n-1);
2542  indexval e=clamp<indexval>(indexval(tn)+1,0,n-1);
2543 
2544  real t=tn-s;
2545  real t1=1.0-t;
2546 
2547  T d1=derivs(s,0);
2548  T d2=derivs(s,1);
2549 
2550  return (ctrls[s]*(t1*t1*t1))+(ctrls[e]*(t*t*t))+(d1*(3*t1*t1*t))+(d2*(3*t1*t*t)); // cubic bezier spline
2551  }
2552 };
2553 
2554 
2555 /*template<typename T>
2556 class ControlCurve
2557 {
2558 protected:
2559  Matrix<T> ctrls;
2560 
2561 public:
2562  ControlCurve() : ctrls("ctrls","",0,1) {}
2563  virtual ~ControlCurve() {}
2564 
2565  virtual void addCtrlPoint(const T& t) { ctrls.append(t); }
2566  virtual void setCtrlPoint(const T& t,indexval index) throw(IndexException) { ctrls.setAt(t,index); }
2567  virtual void removeCtrlPoint(indexval index) throw(IndexException) { ctrls.removeRow(index); }
2568  virtual sval numPoints() const { return ctrls.n(); }
2569  virtual T getCtrlPoint(indexval index) const throw(IndexException) { return ctrls.getAt(index); }
2570 
2571  virtual void setCtrlPoints(const Matrix<T>* pts)
2572  {
2573  ctrls.setN(0);
2574  for(sval i=0;i<pts->n();i++)
2575  ctrls.append(pts->at(i));
2576  }
2577 
2578  virtual void calculateDerivs(){}
2579 
2580  virtual T at(real tt) const
2581  {
2582  indexval n=ctrls.n();
2583  real tn=tt*(n-1);
2584 
2585  indexval s=clamp<indexval>(indexval(tn),0,n-1);
2586  indexval e=clamp<indexval>(indexval(tn)+1,0,n-1);
2587  indexval ds=clamp<indexval>(indexval(tn)-1,0,n-1);
2588  indexval de=clamp<indexval>(indexval(tn)+2,0,n-1);
2589 
2590  real t=tn-s;
2591 
2592  real t2=t*t;
2593  real t3=t2*t;
2594  real t3_05=t3*0.5;
2595  real t3_15=t3*1.5;
2596  real t_05=t*0.5;
2597 
2598  real cs=t3_15-2.5*t2+1;
2599  real ce=2*t2+t_05-t3_15;
2600  real dcs=t2-t_05-t3_05;
2601  real dce=t3_05-0.5*t2;
2602 
2603  T d1=ctrls[ds];
2604  T d2=ctrls[de];
2605 
2606  return ctrls[s]*cs+ctrls[e]*ce+d1*dcs+d2*dce;
2607  }
2608 };*/
2609 
2610 class Vec3Curve : public ControlCurve<vec3>
2611 {
2612  bool isXFunc;
2613  bool isLinear;
2614 public:
2615  Vec3Curve(bool isXFunc) : ControlCurve<vec3>(), isXFunc(isXFunc),isLinear(false) {}
2616 
2617  void setLinear(bool b) { isLinear=b; }
2618  bool isLinearFunc() const { return isLinear; }
2619 
2620  virtual void addCtrlPoint(const vec3& t)
2621  {
2622  vec3 tt=t;
2623  if(isXFunc){
2624  real minx=ctrls.n()==0 ? 0.0 : ctrls[ctrls.n()-1].x();
2625  tt=vec3(clamp<real>(t.x(),minx,1.0),clamp<real>(t.y(),0.0,1.0),0);
2626  }
2628  }
2629 
2630  virtual void setCtrlPoint(const vec3& t,indexval index) throw(IndexException)
2631  {
2632  vec3 tt=t;
2633  if(isXFunc){
2634  real minx=index==0 ? 0.0 : ctrls[index-1].x();
2635  tt=vec3(clamp<real>(t.x(),minx,1.0),clamp<real>(t.y(),0.0,1.0),0);
2636  }
2637 
2638  ctrls.setAt(tt,index);
2639 
2640  if(isXFunc)
2641  for(sval i=index+1;i<ctrls.n();i++)
2642  ctrls[i].x(clamp<real>(ctrls[i].x(),ctrls[i-1].x(),1.0));
2643 
2644  calculateDerivs();
2645  }
2646 
2647  virtual void calculateDerivs()
2648  {
2649  if(isXFunc)
2650  qsort(ctrls.dataPtr(),ctrls.n(),sizeof(vec3),vec3::compX);
2651 
2653  }
2654 
2655  real atX(real x, real threshold=0.0001) const
2656  {
2657  if(x<=ctrls[0].x())
2658  return ctrls[0].y();
2659 
2660  if(x>=ctrls[ctrls.n()-1].x())
2661  return ctrls[ctrls.n()-1].y();
2662 
2663  if(isLinear){
2664  sval i=1;
2665  while(ctrls[i].x()<x)
2666  i++;
2667 
2668  real xi=lerpXi<real>(x,ctrls[i-1].x(),ctrls[i].x());
2669  return lerp<real>(xi,ctrls[i-1].y(),ctrls[i].y());
2670  }
2671  else{
2672  real start=0.0, end=1.0, mid=0.5;
2673 
2674  vec3 val=at(mid);
2675  real diff=val.x()-x;
2676 
2677  // bsearch for interpolated value whose X coord is close to `x'
2678  while(fabs(diff)>threshold && (end-start)>threshold){
2679  if(diff>0)
2680  end=mid;
2681  else
2682  start=mid;
2683 
2684  mid=start+(end-start)*0.5;
2685  val=at(mid);
2686  diff=val.x()-x;
2687  }
2688 
2689  return val.y();
2690  }
2691  }
2692 };
2693 
2694 
2696 {
2697 protected:
2699 
2701 
2702  std::string name;
2703 
2704 public:
2705  Spectrum(const std::string& name="") : alphacurve(true), name(name) {}
2706 
2707  virtual const char* getName() const { return name.c_str(); }
2708 
2709  virtual void clearSpectrum()
2710  {
2711  spec.clear();
2712  alphacurve.clear();
2713  updateSpectrum();
2714  }
2715 
2716  virtual void copySpectrumFrom(const Spectrum* s)
2717  {
2718  spec.copyFrom(&s->spec);
2719  alphacurve.copyFrom(&s->alphacurve);
2720  updateSpectrum();
2721  }
2722 
2723  virtual color getDefaultColor() const { return color(); }
2724 
2725  virtual real getAlpha() const { return 1.0; }
2726 
2727  virtual void updateSpectrum() {}
2728 
2730  virtual void addSpectrumValue(real pos,color value)
2731  {
2732  spec.add(pos,value);
2733  updateSpectrum();
2734  }
2735 
2737  virtual real getSpectrumPos(indexval index) const throw(IndexException)
2738  {
2739  return spec.pos(index);
2740  }
2741 
2743  virtual color getSpectrumValue(indexval index) const throw(IndexException)
2744  {
2745  return spec.get(index);
2746  }
2747 
2749  virtual indexval getSpectrumIndex(real pos,color value) const
2750  {
2751  return spec.find(pos,value);
2752  }
2753 
2755  virtual void setSpectrumValue(sval index, real pos,color value) throw(IndexException)
2756  {
2757  spec.set(index,pos,value);
2758  updateSpectrum();
2759  }
2760 
2762  virtual sval numSpectrumValues() const
2763  {
2764  return spec.size();
2765  }
2766 
2768  virtual void removeSpectrumValue(indexval index) throw(IndexException)
2769  {
2770  spec.remove(index);
2771  updateSpectrum();
2772  }
2773 
2774  virtual sval numAlphaCtrls() const
2775  {
2776  return alphacurve.numPoints();
2777  }
2778 
2779  virtual vec3 getAlphaCtrl(indexval index) const throw(IndexException)
2780  {
2781  return alphacurve.getCtrlPoint(index);
2782  }
2783 
2784  virtual void addAlphaCtrl(vec3 v)
2785  {
2786  alphacurve.addCtrlPoint(v);
2787  updateSpectrum();
2788  }
2789 
2790  virtual void removeAlphaCtrl(indexval index) throw(IndexException)
2791  {
2792  alphacurve.removeCtrlPoint(index);
2793  updateSpectrum();
2794  }
2795 
2796  virtual void setAlphaCtrl(vec3 v, indexval index) throw(IndexException)
2797  {
2798  alphacurve.setCtrlPoint(v,index);
2799  updateSpectrum();
2800  }
2801 
2802  virtual void setAlphaCurve(const Vec3Matrix* pts)
2803  {
2804  alphacurve.setCtrlPoints(pts);
2805  updateSpectrum();
2806  }
2807 
2808  virtual void setLinearAlpha(bool b)
2809  {
2810  alphacurve.setLinear(b);
2811  updateSpectrum();
2812  }
2813 
2814  virtual bool isLinearAlpha() const
2815  {
2816  return alphacurve.isLinearFunc();
2817  }
2818 
2826  virtual color interpolateColor(real pos) const
2827  {
2828  color result;
2829  float alpha=getAlpha();
2830  indexval specsize=spec.size();
2831 
2832  if(specsize==0)
2833  result=getDefaultColor();
2834  else if(pos<=spec.pos(0))
2835  result= spec.get(0);
2836  else if(pos>=spec.pos(specsize-1))
2837  result=spec.get(specsize-1);
2838  else{
2839  sval index=0;
2840  while(index<specsize-1 && spec.pos(index+1)<pos)
2841  index++;
2842 
2843  color cmin=spec.get(index);
2844  color cmax=spec.get(index+1);
2845 
2846  real interp=lerpXi(pos,spec.pos(index),spec.pos(index+1));
2847 
2848  result= cmin.interpolate(interp,cmax);
2849  }
2850 
2851  if(alphacurve.numPoints()>1){
2852  real a=clamp<real>(alphacurve.atX(pos),0.0,1.0);
2853  if(alpha>=0.0 && specsize>0)
2854  a*=alpha;
2855 
2856  result.a(a);
2857  }
2858  else if(alpha>=0.0)
2859  result.a(alpha);
2860 
2861  return result;
2862  }
2863 
2868  virtual void fillColorMatrix(ColorMatrix *col,const RealMatrix *mat,bool useValAsAlpha=false) throw(IndexException)
2869  {
2870  bool hasMatAlpha=mat->m()>=col->m()*2;
2871  sval len=_min(col->n(),mat->n());
2872  sval width=_min(col->m(),mat->m());
2873 
2874  for(sval i=0;i<len;i++){
2875  for(sval j=0;j<width;j++){
2876  float val=mat->getAt(i,j);
2877  color c=interpolateColor(val);
2878 
2879  if(hasMatAlpha)
2880  c.a(mat->getAt(i,col->m()+j)*c.a());
2881  else if(useValAsAlpha)
2882  c.a(val*c.a());
2883 
2884  col->setAt(c,i,j);
2885  }
2886  }
2887  }
2888 };
2889 
2890 
2898 class Material : public Spectrum
2899 {
2900 public:
2901  float alpha;
2902  bool useAlpha;
2903 
2904  Material() : alpha(1.0), useAlpha(true) {}
2905  virtual ~Material(){}
2906 
2908  virtual Material* clone(const char* name) const { return NULL; }
2909 
2911  virtual void copyTo(Material* mat,bool copyTex=false,bool copySpec=false,bool copyProgs=false) const {}
2912 
2914  virtual real getAlpha() const { return useAlpha ? alpha : -1.0; }
2915 
2916  virtual color getDefaultColor() const { return getDiffuse(); }
2917 
2919  virtual void setAlpha(real alpha)
2920  {
2921  this->alpha=(float)alpha;
2922  setDiffuse(getDiffuse());
2923  setSpecular(getSpecular());
2924  }
2925 
2927  virtual bool usesInternalAlpha() const { return useAlpha; }
2928 
2930  virtual void useInternalAlpha(bool val)
2931  {
2932  useAlpha=val;
2933  setDiffuse(getDiffuse());
2934  setSpecular(getSpecular());
2935  }
2936 
2937  virtual color getAmbient() const { return color(); }
2938  virtual color getDiffuse() const { return color(); }
2939  virtual color getSpecular() const { return color(); }
2940  virtual color getEmissive() const { return color(); }
2941 
2942  virtual real getShininess() const { return 0.0f; }
2943  virtual real getPointSizeMin() const { return 0.0f; }
2944  virtual real getPointSizeMax() const { return 0.0f; }
2945  virtual real getPointSizeAbs() const { return 0.0f; }
2946  virtual bool usesPointAttenuation() const { return false; }
2947  virtual BlendMode getBlendMode() const { return BM_ALPHA; }
2948 
2949  virtual bool usesVertexColor() const { return false; }
2950  virtual bool usesLighting() const { return false; }
2951  virtual bool usesFlatShading() const { return false; }
2952  virtual bool usesDepthCheck() const { return false; }
2953  virtual bool usesDepthWrite() const { return false; }
2954  virtual bool usesTexFiltering() const { return false; }
2955  virtual bool isClampTexAddress() const { return false;}
2956  virtual bool isCullBackfaces() const { return false; }
2957  virtual bool usesPointSprites() const { return false; }
2958  virtual const char* getTexture() const { return ""; }
2959  virtual const char* getGPUProgram(ProgramType pt) const { return ""; }
2960 
2961  virtual int getGPUParamInt(ProgramType pt, const std::string& name) { return 0; }
2962  virtual real getGPUParamReal(ProgramType pt, const std::string& name) { return 0; }
2963  virtual vec3 getGPUParamVec3(ProgramType pt, const std::string& name) { return vec3(); }
2964  virtual color getGPUParamColor(ProgramType pt, const std::string& name) { return color(); }
2965 
2967  virtual bool isTransparentColor() const { return useAlpha && alpha<1.0 && !usesVertexColor() && !strcmp(getTexture(),""); }
2968 
2969  virtual void setAmbient(const color & c) {}
2971  virtual void setDiffuse(const color & c){}
2973  virtual void setSpecular(const color & c){}
2974  virtual void setEmissive(const color & c) {}
2976  virtual void setShininess(real c){}
2977 
2979  virtual void setPointSize(real min,real max) {}
2981  virtual void setPointSizeAbs(real size) {}
2983  virtual void setPointAttenuation(bool enabled,real constant=0.0f,real linear=1.0f, real quad=0.0f) {}
2984 
2985  virtual void setBlendMode(BlendMode bm) {}
2986  virtual void useVertexColor(bool use) {}
2987  virtual void useLighting(bool use) {}
2988  virtual void useFlatShading(bool use) {}
2989  virtual void useDepthCheck(bool use) {}
2990  virtual void useDepthWrite(bool use) {}
2991  virtual void useTexFiltering(bool use) {}
2992  virtual void clampTexAddress(bool use) {}
2993  virtual void cullBackfaces(bool cull) {}
2994  virtual void usePointSprites(bool useSprites){}
2995  virtual void setTexture(const char* name){}
2996  virtual void setTexture(const Texture* tex) { setTexture(tex->getName()); }
2997 
2998  virtual void useSpectrumTexture(bool use) {}
2999 
3000  virtual void setGPUProgram(const std::string& name, ProgramType pt) {}
3001  virtual void setGPUProgram(const GPUProgram *prog) { setGPUProgram(prog->getName(),prog->getType()); }
3002 
3003  virtual bool setGPUParamInt(ProgramType pt,const std::string& name, int val) { return false; }
3004  virtual bool setGPUParamReal(ProgramType pt,const std::string& name, real val) { return false; }
3005  virtual bool setGPUParamVec3(ProgramType pt,const std::string& name, vec3 val) { return false; }
3006  virtual bool setGPUParamColor(ProgramType pt,const std::string& name, color val) { return false; }
3007 
3008  virtual void updateSpectrum() {}
3009 };
3010 
3015 class Light
3016 {
3017 public:
3018  Light(){}
3019  virtual ~Light() {}
3020 
3022  virtual void setPosition(vec3 &v){}
3023 
3025  virtual void setDirection(vec3 &v){}
3026 
3028  virtual void setDiffuse(const color & c){}
3029 
3031  virtual void setSpecular(const color & c){}
3032 
3034  virtual void setDirectional() {}
3035 
3037  virtual void setPoint() {}
3038 
3040  virtual void setSpotlight(real radsInner, real radsOuter, real falloff=1.0f) {}
3041 
3043  virtual void setAttenuation(real range, real constant=0.0f,real linear=1.0f, real quad=0.0f) {}
3044 
3046  virtual void setVisible(bool isVisible){}
3047 
3049  virtual bool isVisible() const { return false; }
3050 };
3051 
3057 {
3058 public:
3059  virtual ~VertexBuffer() {}
3060 
3062  virtual vec3 getVertex(int i) const { return vec3(0,0,0); }
3064  virtual vec3 getNormal(int i) const { return vec3(0,0,0); }
3066  virtual color getColor(int i) const { return color(0,0,0,0); }
3068  virtual vec3 getUVWCoord(int i) const { return vec3(0,0,0); }
3069 
3071  virtual sval numVertices() const { return 0; }
3073  virtual bool hasNormal() const { return false; }
3075  virtual bool hasColor() const { return false; }
3077  virtual bool hasUVWCoord() const { return false; }
3078 };
3079 
3082 {
3083 public:
3084  virtual ~IndexBuffer() {}
3085 
3087  virtual sval numIndices() const { return 0; }
3089  virtual sval indexWidth(int i) const { return 0; }
3091  virtual sval getIndex(int i,int w) const { return 0; }
3092 };
3093 
3100 template<typename Ctx>
3102 {
3103 public:
3104  typedef vec3 (*vecfunc)(Ctx,int);
3105  typedef color (*colfunc)(Ctx,int);
3106 
3107  vecfunc vertfunc;
3108  vecfunc normalfunc;
3109  colfunc colorfunc;
3110  vecfunc uvwfunc;
3112  Ctx context;
3113 
3114  CallbackVertexBuffer(Ctx context, sval numvertices, vecfunc vertfunc, vecfunc normalfunc=NULL, colfunc colorfunc=NULL, vecfunc uvwfunc=NULL):
3115  vertfunc(vertfunc),normalfunc(normalfunc),colorfunc(colorfunc),uvwfunc(uvwfunc),numvertices(numvertices),context(context)
3116  { }
3117 
3118  virtual vec3 getVertex(int i) const { return vertfunc(context,i); }
3119  virtual vec3 getNormal(int i) const { return normalfunc(context,i); }
3120  virtual color getColor(int i) const { return colorfunc(context,i); }
3121  virtual vec3 getUVWCoord(int i) const { return uvwfunc(context,i); }
3122 
3123  virtual sval numVertices() const { return numvertices; }
3124  virtual bool hasNormal() const { return normalfunc!=NULL; }
3125  virtual bool hasColor() const { return colorfunc!=NULL; }
3126  virtual bool hasUVWCoord() const { return uvwfunc!=NULL; }
3127 };
3128 
3130 template<typename Ctx>
3132 {
3133 public:
3134  typedef sval (*wfunc)(Ctx,int);
3135  typedef sval (*ifunc)(Ctx,int,int);
3136 
3137  wfunc widthfunc;
3138  ifunc indexfunc;
3139 
3141  Ctx context;
3142 
3143  CallbackIndexBuffer(Ctx context, sval numindices, wfunc widthfunc,ifunc indexfunc):
3144  widthfunc(widthfunc), indexfunc(indexfunc), numindices(numindices), context(context)
3145  {}
3146 
3147  virtual sval numIndices() const { return numindices; }
3148  virtual sval indexWidth(int i) const { return widthfunc(context,i); }
3149  virtual sval getIndex(int i,int w) const { return indexfunc(context,i,w); }
3150 };
3151 
3161 {
3162  Vec3Matrix* vecs;
3163  ColorMatrix* cols;
3164  IndexMatrix* extinds;
3165  sval numverts;
3167 
3168 public:
3170  MatrixVertexBuffer(Vec3Matrix* vecs,ColorMatrix* cols=NULL,IndexMatrix* extinds=NULL) throw(RenderException)
3171  : vecs(vecs), cols(cols),extinds(extinds), deleteMatrices(false)
3172  {
3173  if(vecs==NULL)
3174  throw RenderException("Matrix 'vecs' must be provided");
3175 
3176  numverts=sval(extinds!=NULL ? extinds->n() : vecs->n());
3177  }
3178 
3180  MatrixVertexBuffer(const VertexBuffer* buf) throw(RenderException) : numverts(buf ? buf->numVertices() : 0), cols(NULL), extinds(NULL), deleteMatrices(true)
3181  {
3182  if(!numverts)
3183  throw RenderException("VertexBuffer 'buf' must be provided");
3184 
3185  int columns=1;
3186  if(buf->hasUVWCoord())
3187  columns=4;
3188  else if(buf->hasNormal())
3189  columns=2;
3190 
3191  vecs=new Vec3Matrix("copyvecs",buf->numVertices(),columns);
3192 
3193  if(buf->hasColor())
3194  cols=new ColorMatrix("copycols",buf->numVertices());
3195 
3196  for(sval i=0;i<numverts;i++){
3197  vecs->at(i,0)=buf->getVertex(i);
3198  if(buf->hasNormal())
3199  vecs->at(i,1)=buf->getNormal(i);
3200  if(buf->hasUVWCoord())
3201  vecs->at(i,3)=buf->getUVWCoord(i);
3202 
3203  if(buf->hasColor())
3204  cols->at(i)=buf->getColor(i);
3205  }
3206  }
3207 
3209  {
3210  if(deleteMatrices){
3211  SAFE_DELETE(vecs);
3212  SAFE_DELETE(cols);
3213  SAFE_DELETE(extinds);
3214  }
3215  }
3216 
3217  sval getIndex(sval i) const { return extinds!=NULL ? extinds->at(i) : i; }
3218 
3219  virtual vec3 getVertex(int i) const { return vecs->at(getIndex(i)); }
3220  virtual vec3 getNormal(int i) const { return vecs->at(getIndex(i),1); }
3221  virtual color getColor(int i) const { return cols->at(getIndex(i)); }
3222  virtual vec3 getUVWCoord(int i) const { return vecs->at(getIndex(i),3); }
3223 
3224  virtual sval numVertices() const { return numverts; }
3225  virtual bool hasNormal() const { return vecs->m()>1; }
3226  virtual bool hasColor() const { return cols!=NULL; }
3227  virtual bool hasUVWCoord() const { return vecs->m()>3; }
3228 };
3229 
3232 {
3233  IndexMatrix* indices;
3234  IndexMatrix* extinds;
3236 
3237 public:
3239  MatrixIndexBuffer(IndexMatrix* indices,IndexMatrix* extinds=NULL) : indices(indices), extinds(extinds), deleteMatrices(false)
3240  {}
3241 
3243  MatrixIndexBuffer(const IndexBuffer* buf) throw(RenderException) : extinds(NULL), deleteMatrices(true)
3244  {
3245  if(!buf)
3246  throw RenderException("IndexBuffer 'buf' must be provided");
3247 
3248  indices=new IndexMatrix("copyinds",buf->numIndices(),buf->indexWidth(0));
3249 
3250  for(sval i=0;i<buf->numIndices();i++)
3251  for(sval j=0;j<_min(indices->m(),buf->indexWidth(i));j++)
3252  indices->at(i,j)=buf->getIndex(i,j);
3253  }
3254 
3256  {
3257  if(deleteMatrices){
3258  SAFE_DELETE(indices);
3259  SAFE_DELETE(extinds);
3260  }
3261  }
3262 
3263  virtual sval numIndices() const
3264  {
3265  if(indices==NULL)
3266  return 0;
3267  else if(extinds!=NULL)
3268  return sval(extinds->n());
3269  else
3270  return sval(indices->n());
3271  }
3272  virtual sval indexWidth(int i) const { return sval(indices!=NULL ? indices->m() : 0); }
3273  virtual sval getIndex(int i,int j) const { return sval(indices->getAt(extinds!=NULL ? extinds->getAt(i) : i,j)); }
3274 };
3275 
3277 class Ray
3278 {
3279  vec3 pos,dir,invdir;
3280  bool signx,signy,signz;
3281 public:
3282 
3284  {}
3285 
3286  Ray(const vec3 &pos, const vec3 &dir): pos(pos)
3287  {
3288  setDirection(dir);
3289  }
3290 
3291  Ray(const Ray& r) : pos(r.pos)
3292  {
3293  setDirection(r.dir);
3294  }
3295 
3297  vec3 getPosition(real t=0) const { return pos+(dir*t); }
3298 
3300  vec3 getDirection() const { return dir; }
3301 
3303  void setPosition(const vec3 &v) { pos=v; }
3304 
3306  void setDirection(const vec3 &v)
3307  {
3308  if(v.isZero())
3309  throw std::invalid_argument("Direction vector is zero length.");
3310 
3311  dir=v.norm();
3312 
3313  // store the direction inverse for bound box check efficiency
3314  invdir=dir.inv();
3315 
3316  // store component signs for bound box check efficiency
3317  signx=invdir.x()<0;
3318  signy=invdir.y()<0;
3319  signz=invdir.z()<0;
3320  }
3321 
3323  real distTo(const vec3 v) const { return dir.dot(v-pos); }
3324 
3326  bool onPlane(const vec3& planept, const vec3& planenorm) const
3327  {
3328  return pos.onPlane(planept,planenorm) && (pos+dir).onPlane(planept,planenorm);
3329  }
3330 
3336  real intersectsPlane(const vec3 & planepos, const vec3 & planenorm) const
3337  {
3338  return planenorm.dot(planepos-pos)/planenorm.dot(dir);
3339  }
3340 
3346  realpair intersectsAABB(const vec3& minv, const vec3& maxv) const
3347  {
3348  real tmin, tmax, tymin, tymax, tzmin, tzmax;
3349 
3350  tmin = ((signx ? maxv : minv).x() - pos.x()) * invdir.x();
3351  tmax = ((signx ? minv : maxv).x() - pos.x()) * invdir.x();
3352  tymin = ((signy ? maxv : minv).y() - pos.y()) * invdir.y();
3353  tymax = ((signy ? minv : maxv).y() - pos.y()) * invdir.y();
3354 
3355  if ( (tmin > tymax) || (tymin > tmax) )
3356  return realpair(-1,-1);
3357 
3358  if (tymin > tmin)
3359  tmin = tymin;
3360  if (tymax < tmax)
3361  tmax = tymax;
3362 
3363  tzmin = ((signz ? maxv : minv).z() - pos.z()) * invdir.z();
3364  tzmax = ((signz ? minv : maxv).z() - pos.z()) * invdir.z();
3365 
3366  if ( (tmin > tzmax) || (tzmin > tmax) )
3367  return realpair(-1,-1);
3368 
3369  if (tzmin > tmin)
3370  tmin = tzmin;
3371  if (tzmax < tmax)
3372  tmax = tzmax;
3373 
3374  return realpair(tmin,tmax);//tmin < to && tmax > from;
3375  }
3376 
3383  realpair intersectsSphere(const vec3& center, real rad) const
3384  {
3385  real tca = distTo(center); // distance from `pos' to projection of `center' on the ray
3386  real thc=0; // offset from project of `center' on the ray where the sphere is intersected
3387 
3388  if(tca>0){ // if tca is positive, the sphere is in front of the ray, otherwise the result is where the sphere center projects on the ray
3389  real r2=rad*rad;
3390  vec3 L = center - pos;
3391  real d2 = L.dot(L) - tca * tca;
3392 
3393  if (d2 < r2) // ray intersects sphere at 2 points offset by thc, otherwise ray passes outside of sphere
3394  thc = sqrt(r2 - d2);
3395  }
3396 
3397  return realpair(tca - thc, tca + thc); // thc==0 if ray does not pass through sphere, so tca is projection distance either in front or behind
3398  }
3399 
3405  realpair intersectsRay(const Ray& ray) const
3406  {
3407  real t=0,s=0;
3408  vec3 p1=pos;
3409  vec3 p2=ray.getPosition();
3410  real t1=distTo(p2);
3411  real t2=ray.distTo(p1);
3412  vec3 pt1=getPosition(t1);
3413  vec3 pt2=ray.getPosition(t2);
3414 
3415  if(p2==pt1 || p1==pt2){ // check if either ray is pointing at the other
3416  if(p2==pt1) // this was pointing at `ray'
3417  t=t1;
3418  if(p1==pt2) // `ray' was pointing at this
3419  s=t2;
3420  }
3421  else{
3422  vec3 norm=p1.planeNorm(p2,getPosition(1.0)); // calculate a normal based on the two origins at the point at t=1.0
3423  if(ray.getPosition(1.0).onPlane(p1,norm)){ // if the point for `ray' at t=1.0 is on the plane then the rays intersect if they aren't parallel
3424  vec3 rd=ray.getDirection();
3425  real angle=dir.angleTo(rd);
3426 
3427  if(angle>dEPSILON && angle<(dPI-dEPSILON)){ // rays are not parallel
3428  t=intersectsPlane(p2,rd.cross(norm)); // find where this intersects the plane defined by `ray' at right angle to the plane this and `ray' lie on
3429  s=ray.distTo(getPosition(t)); // find the intersect distance for `ray' by seeing how far away the interect point for this is
3430  }
3431  }
3432  }
3433 
3434  return realpair(t,s);
3435  }
3436 
3438  real intersectsLineSeg(const vec3& v1, const vec3& v2) const
3439  {
3440  real dist=intersectsPlane(v1,pos.planeNorm(v1,v2).cross(v2-v1));
3441 
3442  if(dist<0 || dist==realInf)
3443  return -1;
3444 
3445  vec3 lpos=getPosition(dist);
3446 
3447  if(lpos==v1 || lpos==v2 || equalsEpsilon(lpos.lineDist(v1,v2),0))
3448  return dist;
3449 
3450  //if(dist>=0 && dist<realInf && equalsEpsilon(lpos.lineDist(v1,v2),0))
3451  // return dist;
3452 
3453  return -1;
3454  }
3455 
3463  realtriple intersectsTri(const vec3& v0, const vec3& v1, const vec3& v2) const
3464  {
3465  vec3 e1=v1-v0; // triangle edge 1
3466  vec3 e2=v2-v0; // triangle edge 2
3467  vec3 p=dir.cross(e2); // direction perpendicular to ray line and edge 2
3468  real det=e1.dot(p); // determinant, will be 0 if edge 1 and p are perpendicular
3469 
3470  if(equalsEpsilon(det,0.0)) // ray is parallel with triangle's plane
3471  return realtriple(-1,-1,-1);
3472 
3473  real invdet=1.0/det;
3474 
3475  vec3 t=pos-v0;
3476  real u=p.dot(t)*invdet;
3477 
3478  if(u < 0 || u > 1) // ray intersects triangle's plane outside the band parallel with e1
3479  return realtriple(-1,-1,-1);
3480 
3481  vec3 q=t.cross(e1);
3482  real v=dir.dot(q)*invdet;
3483 
3484  if(v < 0 || u + v > 1) // ray intersects triangle's plane outside the band parallel with e2 or outside the triangle area
3485  return realtriple(-1,-1,-1);
3486 
3487  real len=e2.dot(q)*invdet;
3488 
3489  if(len>dEPSILON) // ray points away from triangle
3490  return realtriple(len,u,v);
3491 
3492  return realtriple(-1,-1,-1);
3493  }
3494 
3506  std::vector<indextriple> intersectsTriMesh(const Vec3Matrix* const nodes, const IndexMatrix* const inds,
3507  const Vec3Matrix* const centers, const RealMatrix* const radii2, sval numResults=0,sval excludeInd=-1) const throw(IndexException)
3508  {
3509  std::vector<indextriple> results;
3510  sval len=inds->n();
3511 
3512  if(centers && radii2)
3513  len=_min(len,_min(centers->n(),radii2->n()));
3514 
3515  for(sval n=0;n<len && (numResults==0 || results.size()<numResults);n++){
3516  if(n==excludeInd)
3517  continue;
3518 
3519  vec3 ncenter,npos,v0,v1,v2;
3520  real nrad=0,cdist;
3521 
3522  if(centers && radii2){
3523  ncenter=centers->at(n);
3524  nrad=radii2->at(n);
3525  //npos=getPosition(ncenter.distTo(pos));
3526 
3527  //if(npos.distToSq(ncenter)>nrad)
3528  // continue;
3529 
3530  cdist=distTo(ncenter);
3531  if((cdist*cdist)>nrad)
3532  continue;
3533 
3534  v0=nodes->getAt(inds->at(n,0));
3535  v1=nodes->getAt(inds->at(n,1));
3536  v2=nodes->getAt(inds->at(n,2));
3537  }
3538  else{
3539  v0=nodes->getAt(inds->at(n,0));
3540  v1=nodes->getAt(inds->at(n,1));
3541  v2=nodes->getAt(inds->at(n,2));
3542 
3543  ncenter=(v0+v1+v2)/3.0;
3544  nrad=_max(ncenter.distToSq(v0),_max(ncenter.distToSq(v1),ncenter.distToSq(v2)));
3545  npos=getPosition(ncenter.distTo(pos));
3546 
3547  if(npos.distToSq(ncenter)>nrad)
3548  continue;
3549  }
3550 
3551  realtriple inter=intersectsTri(v0,v1,v2);
3552 
3553  if(inter.first>=0)
3554  results.push_back(indextriple(indexval(n),inter));
3555  }
3556 
3557  return results;
3558  }
3559 };
3560 
3570 {
3571 public:
3576 
3578  transform(const vec3& trans=vec3(),const vec3& scale=vec3(1.0),const rotator& rot=rotator(),bool isInv=false) :
3579  trans(trans),scale(scale),rot(rot),_isInverse(isInv)
3580  {}
3581 
3582  transform(const transform & t) : trans(t.trans),scale(t.scale),rot(t.rot),_isInverse(t.isInverse())
3583  {}
3584 
3586  transform(real x, real y, real z, real sx, real sy, real sz, real yaw, real pitch, real roll, bool isInv=false) :
3587  trans(x,y,z), scale(sx,sy,sz),rot(yaw,pitch,roll), _isInverse(isInv)
3588  {}
3589 
3590  vec3 getTranslation() const { return trans; }
3591  vec3 getScale() const { return scale; }
3592  rotator getRotation() const { return rot; }
3593 
3594  bool isInverse() const { return _isInverse; }
3595 
3596  void setTranslation(const vec3 &v){ trans=v; }
3597  void setScale(const vec3 &v) { scale=v; }
3598  void setRotation(const rotator &r) { rot=r; }
3599 
3601  vec3 operator * (const vec3 &v) const
3602  {
3603  if(_isInverse)
3604  return scale*(rot*(v+trans));
3605  else
3606  return trans+(rot*(v*scale));
3607  }
3608 
3610  friend vec3 operator * (const vec3 &v, const transform &t)
3611  {
3612  return t*v;
3613  }
3614 
3616  vec3 operator / (const vec3 &v) const
3617  {
3618  return inverse()*v;
3619  }
3620 
3622  friend vec3 operator / (const vec3 &v, const transform &t)
3623  {
3624  return t/v;
3625  }
3626 
3627  friend vec3& operator *= (vec3 &v, const transform &t)
3628  {
3629  vec3 vv=t*v;
3630  v.x(vv.x());
3631  v.y(vv.y());
3632  v.z(vv.z());
3633  return v;
3634  }
3635 
3637  Ray operator * (const Ray &r) const
3638  {
3639  transform t=*this;
3640  return Ray(t*r.getPosition(),t.directional()*r.getDirection());
3641  }
3642 
3644  friend Ray operator * (const Ray &r, const transform &t)
3645  {
3646  return t*r;
3647  }
3648 
3658  transform operator * (const transform& t) const
3659  {
3660  /*vec3 nscale=t.getScale()*scale;
3661  rotator nrot=rot*t.getRotation();
3662  vec3 ntrans=t.getTranslation()+(trans*t.directional());
3663  return transform(ntrans,nscale,nrot);*/
3664  transform _t=*this;
3665  vec3 mincorner=_t*(t*vec3(0));
3666  vec3 maxcorner=_t*(t*vec3(1));
3667  vec3 xcorner=_t*(t*vec3(1,0,0));
3668  vec3 ycorner=_t*(t*vec3(0,1,0));
3669  rotator rot((xcorner-mincorner).norm(),(ycorner-mincorner).norm(),vec3(1,0,0),vec3(0,1,0));
3670  vec3 scale=rot/(maxcorner-mincorner);
3671  return transform(mincorner,scale,rot);
3672  }
3673 
3674  bool operator == (const transform & t) const
3675  {
3676  return trans==t.trans && scale==t.scale && rot==t.rot && _isInverse==t._isInverse;
3677  }
3678 
3681  {
3682  return transform(trans*-1,scale.inv(),rot.inverse(),!_isInverse);
3683  }
3684 
3687  {
3688  return transform(vec3(),scale,rot,_isInverse);
3689  }
3690 
3692  void toMatrix(real* mat) const
3693  {
3694  rot.toMatrix(mat);
3695  if(_isInverse){
3696  mat[ 0]*=scale.x();
3697  mat[ 1]*=scale.x();
3698  mat[ 2]*=scale.x();
3699  mat[ 4]*=scale.y();
3700  mat[ 5]*=scale.y();
3701  mat[ 6]*=scale.y();
3702  mat[ 8]*=scale.z();
3703  mat[ 9]*=scale.z();
3704  mat[10]*=scale.z();
3705  mat[ 3]=trans.dot(vec3(mat[0],mat[1],mat[2]));
3706  mat[ 7]=trans.dot(vec3(mat[4],mat[5],mat[6]));
3707  mat[11]=trans.dot(vec3(mat[8],mat[9],mat[10]));
3708  }
3709  else{
3710  mat[ 0]*=scale.x();
3711  mat[ 1]*=scale.y();
3712  mat[ 2]*=scale.z();
3713  mat[ 4]*=scale.x();
3714  mat[ 5]*=scale.y();
3715  mat[ 6]*=scale.z();
3716  mat[ 8]*=scale.x();
3717  mat[ 9]*=scale.y();
3718  mat[10]*=scale.z();
3719  mat[ 3]=trans.x();
3720  mat[ 7]=trans.y();
3721  mat[11]=trans.z();
3722  }
3723  }
3724 
3725  mat4 toMatrix() const
3726  {
3727  mat4 m;
3728  toMatrix((real*)m.m);
3729  return m;
3730  }
3731 
3732  friend std::ostream& operator << (std::ostream &out, const transform &t)
3733  {
3734  return out << "transform(" << t.getTranslation() << ", " << t.getScale() << ", " << t.getRotation() << ", " << (t.isInverse() ? "true" : "false") << ")";
3735  }
3736 };
3737 
3739 class Image
3740 {
3741 public:
3742  virtual ~Image() {}
3743 
3745  virtual TextureFormat getFormat() const { return TF_UNKNOWN; }
3747  virtual sval getWidth() const { return 0; }
3749  virtual sval getHeight() const { return 0; }
3751  virtual sval getDepth() const { return 0; }
3753  virtual size_t getDataSize() const { return 0; }
3755  virtual u8* getData() { return 0; }
3757  virtual std::string encode(const std::string& format) { return ""; }
3759  virtual void fillRealMatrix(RealMatrix* mat) throw(IndexException) {}
3761  virtual void fillColorMatrix(ColorMatrix* mat) throw(IndexException) {}
3762 };
3763 
3772 class Camera
3773 {
3774 public:
3776  virtual ~Camera() {}
3777 
3778  virtual const char* getName() const { return ""; }
3779 
3781  virtual real getAspectRatio() const { return 0; }
3782 
3788  virtual Ray* getProjectedRay(real x, real y, bool isAbsolute=true) const { return NULL; }
3789 
3790  virtual vec3 getPosition() const { return vec3();}
3791  virtual vec3 getLookAt() const { return vec3();}
3792  virtual rotator getRotation() const { return rotator(); }
3793 
3794  virtual mat4 getViewMatrix() const { return mat4(); }
3795  virtual mat4 getProjMatrix() const { return mat4(); }
3796 
3798  virtual vec3 getScreenPosition(vec3 pos) const { return vec3(); }
3799 
3801  virtual vec3 getWorldPosition(real x, real y, bool isAbsolute=true) const
3802  {
3803  Ray *r=getProjectedRay(x,y,isAbsolute);
3804  vec3 pos=r->getPosition();
3805  delete r;
3806  return pos;
3807  }
3808 
3809  virtual void setPosition(const vec3 &v){}
3810  virtual void setLookAt(const vec3 &v) {}
3811  virtual void setUp(const vec3 & v){}
3812  virtual void setZUp(){}
3813  virtual void rotate(const rotator & r) {}
3814  virtual void setRotation(const rotator& r){}
3815 
3816  virtual void setNearClip(real dist) {}
3817  virtual void setFarClip(real dist) {}
3818  virtual void setVertFOV(real rads) {}
3819  virtual void setBGColor(const color & c) {}
3820  virtual void setAspectRatio(real rat) {}
3821  virtual void setViewport(real left=0.0f,real top=0.0f,real width=1.0f,real height=1.0f) {}
3822  virtual void setOrtho(bool isOrtho){}
3823  virtual void setWireframe(bool isWireframe){}
3824  virtual void setSecondaryCamera(bool selective){}
3825 
3826  virtual real getVertFOV() const { return 0; }
3827 
3828  virtual real getNearClip() const {return 0; }
3829  virtual real getFarClip() const {return 0; }
3830 
3831  virtual sval getWidth() const { return 0; }
3832  virtual sval getHeight() const { return 0; }
3833 
3834  virtual bool isPointInViewport(int x, int y) const { return false;}
3835 
3836  virtual bool isSecondaryCamera() { return false; }
3837 
3839  virtual void renderToFile(const std::string& filename,sval width,sval height, TextureFormat format=TF_RGB24,real stereoOffset=0.0) throw(RenderException) {}
3841  virtual void renderToStream(void* stream,sval width,sval height, TextureFormat format=TF_RGB24,real stereoOffset=0.0) throw(RenderException) {}
3843  virtual Image* renderToImage(sval width,sval height, TextureFormat format=TF_RGB24,real stereoOffset=0.0) throw(RenderException) { return 0; }
3844 };
3845 
3850 class Figure
3851 {
3852 public:
3853  virtual ~Figure(){}
3854 
3856  virtual const char* getName() {return "";}
3858  virtual void setPosition(const vec3 &v) {}
3860  virtual void setScale(const vec3 &v) {}
3862  virtual void setRotation(const rotator& r){}
3864  virtual void setTransform(const transform &t) { setTransform(t.trans,t.scale,t.rot); }
3866  virtual void setTransform(const vec3 &trans,const vec3 &scale, const rotator &rot)
3867  {
3868  setPosition(trans);
3869  setRotation(rot);
3870  setScale(scale);
3871  }
3872 
3874  virtual vec3 getPosition(bool isDerived=false) const { return vec3(); }
3876  virtual vec3 getScale(bool isDerived=false) const { return vec3(); }
3878  virtual rotator getRotation(bool isDerived=false) const { return rotator(); }
3880  virtual transform getTransform(bool isDerived=false) const { return transform(getPosition(isDerived),getScale(isDerived),getRotation(isDerived),false); }
3881 
3883  virtual void setMaterial(const char* mat) throw(RenderException) {}
3885  virtual void setMaterial(const Material *mat) throw(RenderException) { setMaterial(mat->getName()); }
3886 
3888  virtual const char* getMaterial() const { return ""; }
3889 
3890  virtual std::pair<vec3,vec3> getAABB() const { return std::pair<vec3,vec3>(vec3(),vec3()); }
3891 
3899  virtual void fillData(const VertexBuffer* vb, const IndexBuffer* ib,bool deferFill=false,bool doubleSided=false) throw(RenderException) {}
3900 
3902  virtual void setVisible(bool isVisible){}
3904  virtual bool isVisible() const { return false;}
3905 
3907  virtual bool isTransparent() const { return false; }
3909  virtual bool isOverlay() const { return false; }
3910 
3912  virtual void setTransparent(bool isTrans){}
3914  virtual void setOverlay(bool isOverlay){}
3915 
3917  virtual void setRenderQueue(sval queue){}
3919  virtual sval getRenderQueue() const { return 0; }
3920 
3928  virtual void setCameraVisibility(const Camera* cam, bool isVisible){}
3929 
3931  virtual void setParent(Figure *fig){}
3932 };
3933 
3935 class BBSetFigure : public Figure
3936 {
3937 public:
3938  virtual ~BBSetFigure(){}
3939 
3940  virtual void setDimension(real width, real height){}
3941 
3942  virtual real getWidth() const { return 0;}
3943  virtual real getHeight() const { return 0;}
3944 
3945  virtual void setUpVector(const vec3& v){}
3946 
3947  virtual int numBillboards() const {return 0;}
3948 
3949  virtual void fillData(const VertexBuffer* vb, const IndexBuffer* ib,bool deferFill=false,bool doubleSided=false) throw(RenderException) {}
3950 
3951  virtual void setBillboardPos(indexval index, const vec3& pos) throw(IndexException) {}
3952  virtual void setBillboardDir(indexval index, const vec3& dir) throw(IndexException) {}
3953  virtual void setBillboardColor(indexval index, const color& col) throw(IndexException) {}
3954 };
3955 
3957 {
3958 public:
3960 
3961  virtual void setNumPlanes(sval num){}
3962  virtual sval getNumPlanes() const { return 0; }
3963 
3964  virtual void setAlpha(real a) {}
3965  virtual real getAlpha() const { return 0; }
3966 
3967  virtual void setTexAABB(const vec3& minv, const vec3& maxv) {}
3968 
3969  virtual void setAABB(const vec3& minv, const vec3& maxv) {}
3970  //virtual void setTexCoordDir(bool invertX, bool invertY, bool invertZ) {}
3971 
3972  virtual vec3 getTexXiPos(vec3 pos) const { return vec3();}
3973 
3974  virtual vec3 getTexXiDir(vec3 pos) const { return vec3();}
3975 
3976  virtual sval getPlaneIntersects(vec3 planept, vec3 planenorm,vec3 buffer[6][2],bool transformPlane=false,bool isXiPoint=false)
3977  {
3978  return 0;
3979  }
3980 };
3981 
3982 class GlyphFigure : public Figure
3983 {
3984 public:
3985  virtual ~GlyphFigure() {}
3986 
3987  virtual void setGlyphScale(vec3 v) {}
3988  virtual vec3 getGlyphScale() const { return vec3(); }
3989  virtual void setGlyphName(const std::string& name) {}
3990  virtual std::string getGlyphName() const {return ""; }
3991  virtual void addGlyphMesh(const std::string& name,const Vec3Matrix* nodes, const Vec3Matrix* norms, const IndexMatrix* inds) {}
3992 };
3993 
3994 class RibbonFigure : public Figure
3995 {
3996 public:
3997  virtual ~RibbonFigure() {}
3998  virtual void setOrientation(const vec3& orient) {}
3999  virtual bool isCameraOriented() const { return true; }
4000  virtual vec3 getOrientation() const { return vec3(); }
4001 
4002  virtual void setNumRibbons(sval num) {}
4003  virtual sval numRibbons() const { return 0; }
4004  virtual sval numNodes(sval ribbon) const throw(IndexException) { return 0; }
4005  virtual void setMaxNodes(sval num) {}
4006  virtual sval getMaxNodes() const { return 0; }
4007 
4008  virtual void clearRibbons() {}
4009  virtual void removeRibbon(sval ribbon) throw(IndexException) {}
4010  virtual void removeNode(sval ribbon) throw(IndexException) {}
4011  virtual void addNode(sval ribbon,const vec3& pos, const color& col,real width, const rotator& rot=rotator(), real tex=0.0) throw(IndexException) {}
4012  virtual void setNode(sval ribbon,sval node,const vec3& pos, const color& col,real width, const rotator& rot=rotator(), real tex=0.0) throw(IndexException) {}
4013  virtual vec3 getNode(sval ribbon,sval node) throw(IndexException) { return vec3(); }
4015 };
4016 
4017 class TextFigure : public Figure
4018 {
4019 public:
4020  virtual ~TextFigure() {}
4021  virtual void setText(const std::string& text) {}
4022  virtual void setFont(const std::string& fontname) {}
4023  virtual void setColor(const color& col) {}
4024 
4025  virtual void setVAlign(VAlignType align){}
4026  virtual void setHAlign(HAlignType align){}
4027  virtual void setTextHeight(real height){}
4028  virtual void setSpaceWidth(real width) {}
4029 
4030  virtual void setCameraAlign(bool align) {}
4031 
4032  virtual std::string getText() const { return "";}
4033  virtual std::string getFont() const { return "";}
4034  virtual color getColor() const { return color(); }
4035 
4036  virtual VAlignType getVAlign() const { return V_CENTER; }
4037  virtual HAlignType getHAlign() const { return H_CENTER; }
4038  virtual real getTextHeight() const { return 0; }
4039  virtual real getSpaceWidth() const { return 0; }
4040 
4041  virtual bool isCameraAligned() const { return false; }
4042 };
4043 
4051 class Config
4052 {
4053 protected:
4054  typedef std::pair<std::string,std::string> strpair;
4055  typedef std::map<strpair,std::string> configmap;
4056 
4057  configmap map;
4058 
4059 public:
4061 
4062  void set(const char* group, const char* name, const char* value) { map[getPair(group,name)]=value; }
4063  void set(const char* name, const char* value) { map[getPair("",name)]=value; }
4064  bool hasValue(const char* group, const char* name) const { return map.find(getPair(group,name))!=map.end(); }
4065  bool hasValue(const char* name) const { return hasValue("",name); }
4066  const char* get(const char* group, const char* name) { return hasValue(group,name) ? map[getPair(group,name)].c_str() : ""; }
4067  const char* get(const char* name) { return get("",name); }
4068 
4069  std::string toString()
4070  {
4071  std::ostringstream out;
4072  for(configmap::const_iterator i=map.begin();i!=map.end();i++)
4073  out << "(" <<(*i).first.first << ", " << (*i).first.second << ") = " << (*i).second << std::endl;
4074 
4075  return out.str();
4076  }
4077 
4078  friend std::ostream& operator << (std::ostream &out, const Config *c)
4079  {
4080  for(configmap::const_iterator i=c->map.begin();i!=c->map.end();i++)
4081  out << "(" <<(*i).first.first << ", " << (*i).first.second << ") = " << (*i).second << std::endl;
4082 
4083  return out;
4084  }
4085 
4086 private:
4087  strpair getPair(const char* group, const char* name) const
4088  {
4089  std::string sgroup=group,sname=name;
4090  std::transform(sgroup.begin(), sgroup.end(), sgroup.begin(), ::tolower);
4091  std::transform(sname.begin(), sname.end(), sname.begin(), ::tolower);
4092  return strpair(sgroup,sname);
4093  }
4094 };
4095 
4102 {
4105 
4106 public:
4107  RenderScene() : renderHighQuality(false), alwaysHighQuality(false) {}
4108 
4109  virtual ~RenderScene() {}
4110 
4116  virtual Camera* createCamera(const char* name,real left=0.0f,real top=0.0f,real width=1.0f,real height=1.0f) throw(RenderException) { return NULL; }
4117 
4119  virtual void setAmbientLight(const color & c) {}
4120 
4122  virtual void addResourceDir(const char* dir) {}
4123 
4125  virtual void initializeResources() {}
4126 
4128  virtual Material* createMaterial(const char* name) throw(RenderException) {return NULL;}
4129 
4131  virtual Figure* createFigure(const char* name, const char* mat,FigureType type) throw(RenderException) {return NULL;}
4132 
4134  virtual Light* createLight() throw(RenderException) { return NULL; }
4135 
4137  virtual Image* loadImageFile(const std::string &filename) throw(RenderException) { return NULL; }
4138 
4140  virtual Texture* createTexture(const char* name,sval width, sval height, sval depth, TextureFormat format) throw(RenderException) { return NULL; }
4141 
4143  virtual Texture* loadTextureFile(const char* name,const char* absFilename) throw(RenderException) { return NULL; }
4144 
4146  virtual GPUProgram* createGPUProgram(const char* name,ProgramType ptype,const char* language) throw(RenderException) { return NULL; }
4147 
4149  virtual void saveScreenshot(const char* filename,Camera* c=NULL,int width=0,int height=0,real stereoOffset=0.0,TextureFormat tf=TF_RGB24) throw(RenderException) {}
4150 
4152  virtual Config* getConfig() const {return NULL; }
4153 
4155  virtual void logMessage(const char* msg){}
4156 
4158  virtual void setBGObject(color col,bool enabled){}
4159 
4161  void setRenderHighQuality(bool val) { renderHighQuality=val; }
4162 
4164  void setAlwaysHighQuality(bool val) { alwaysHighQuality=val; }
4165 
4167  bool getRenderHighQuality() const { return renderHighQuality || alwaysHighQuality;}
4168 
4170  bool getAlwaysHighQuality() const { return alwaysHighQuality; }
4171 };
4172 
4200 {
4201 public:
4202  virtual ~RenderAdapter(){}
4203 
4204  virtual u64 createWindow(int width, int height) throw(RenderException) { return 0; }
4205  virtual void paint(){}
4206  virtual void resize(int x, int y,int width, int height){}
4207  virtual RenderScene* getRenderScene() { return NULL; }
4208 };
4209 
4215 
4216 /*****************************************************************************************************************************/
4217 /* Algorithms */
4218 /*****************************************************************************************************************************/
4219 
4220 template<typename T,typename V> void setMatrixMinMax(Matrix<T>* mat,const V& minv,const V& maxv)
4221 {
4222  std::ostringstream os;
4223  os << V(minv);
4224  mat->meta("min",os.str().c_str());
4225  os.str("");
4226  os << V(maxv);
4227  mat->meta("max",os.str().c_str());
4228 }
4229 
4231 template<typename T> struct StrConvert { static T conv(const char* str) { return T(); } };
4232 template<> struct StrConvert<real> { static real conv(const char* str) { return atof(str); } };
4233 template<> struct StrConvert<indexval> { static indexval conv(const char* str) { return atol(str); } };
4234 
4236 template<typename T> struct ParseLine
4237 {
4238  static void parse(const char* line,sval numvals,T* list)
4239  {
4240  char *buf=new char[strlen(line)+1];
4241  strcpy(buf,line);
4242 
4243  char* p=strtok(buf," \t\r\n");
4244 
4245  for(sval x=0;x<numvals && p;x++){
4246  list[x]=StrConvert<T>::conv(p);
4247  p=strtok(NULL," \t\r\n");
4248  }
4249 
4250  delete buf;
4251  }
4252 };
4253 
4255 template<> struct ParseLine<vec3>
4256 {
4257  static void parse(const char* line,sval numvals,vec3* list)
4258  {
4259  char *buf=new char[strlen(line)+1];
4260  strcpy(buf,line);
4261 
4262  char* p=strtok(buf," \t\r\n");
4263 
4264  for(sval i=0;i<numvals && p;i++){
4265  real x=StrConvert<real>::conv(p);
4266  p=strtok(NULL," \t\r\n");
4267  real y=(p ? StrConvert<real>::conv(p) : 0);
4268  p=strtok(NULL," \t\r\n");
4269  real z=(p ? StrConvert<real>::conv(p) : 0);
4270  p=strtok(NULL," \t\r\n");
4271  list[i]=vec3(x,y,z);
4272  }
4273 
4274  delete buf;
4275  }
4276 };
4277 
4279 template<typename T>
4280 void readTextFileMatrix(const std::string & filename, sval numHeaders, Matrix<T>* mat)
4281 {
4282  std::ifstream in(filename.c_str());
4283 
4284  if(!in)
4285  return;
4286 
4287  std::string line;
4288  sval numvals=sval(mat->m());
4289  T* entry=new T[numvals];
4290 
4291  if(numHeaders>0){
4292  sval* header=new sval[numHeaders];
4293  std::getline(in,line);
4294  ParseLine<sval>::parse(line.c_str(),numHeaders,header);
4295  delete header;
4296  }
4297 
4298  while(std::getline(in,line)){
4299  if(line.find_first_not_of(" \n\t\r")==std::string::npos)
4300  continue;
4301 
4302  ParseLine<T>::parse(line.c_str(),numvals,entry);
4303 
4304  mat->append(entry[0]);
4305  sval pos=mat->n()-1;
4306  for(sval x=1;x<numvals;x++)
4307  mat->at(pos,x)=entry[x];
4308  }
4309 
4310  delete entry;
4311 }
4312 
4313 
4315 //template<typename T> void convertStreamToRealMatrix(const T* stream, RealMatrix* mat);
4316 template<typename T>
4317 void convertStreamToRealMatrix(const T* stream, RealMatrix* mat)
4318 {
4319  T minval=stream[0];
4320  T maxval=minval;
4321  sval mn=mat->n(),mm=mat->m();
4322 
4323  for(sval n=0;n<mn;n++)
4324  for(sval m=0;m<mm;m++){
4325  T val=stream[n*mm+m]; // totally unsafe if stream is too short
4326  minval=_min(val,minval);
4327  maxval=_max(val,maxval);
4328 
4329  mat->at(n,m)=val;
4330  }
4331 
4332  setMatrixMinMax<real,real>(mat,minval,maxval);
4333 }
4334 
4335 void convertUByteStreamToRealMatrix(const char* stream, RealMatrix* mat);
4336 void convertUShortStreamToRealMatrix(const char* stream, RealMatrix* mat);
4337 void convertByteStreamToRealMatrix(const char* stream, RealMatrix* mat);
4338 void convertShortStreamToRealMatrix(const char* stream, RealMatrix* mat);
4339 void convertUIntStreamToRealMatrix(const char* stream, RealMatrix* mat);
4340 void convertIntStreamToRealMatrix(const char* stream, RealMatrix* mat);
4341 void convertFloatStreamToRealMatrix(const char* stream, RealMatrix* mat);
4342 void convertRealStreamToRealMatrix(const char* stream, RealMatrix* mat);
4343 //void convertRGBA32StreamToRealMatrix(const u8* stream, RealMatrix* mat);
4344 
4345 //RealMatrix* readImageFile(const std::string& filename);
4346 
4347 std::pair<vec3,vec3> calculateBoundBox(const Vec3Matrix* mat);
4348 
4349 
4356 realtriple calculateTriPlaneSlice(const vec3& planept, const vec3& planenorm, const vec3& a, const vec3& b, const vec3& c);
4357 real calculateLinePlaneSlice(const vec3& planept, const vec3& planenorm, const vec3& a, const vec3& b);
4358 
4359 void calculateTetValueIntersects(real val, real a, real b, real c, real d, real* results);
4360 
4361 sval calculateHexValueIntersects(real val,const real* vals,intersect* results);
4362 
4364 void basis_Tet1NL(real xi0, real xi1, real xi2, real* coeffs);
4365 
4367 void basis_Hex1NL(real xi0, real xi1, real xi2, real* coeffs);
4368 
4369 real basis_n_NURBS(sval ctrlpt,sval degree, real xi,const RealMatrix *knots);
4370 
4371 void basis_NURBS_default(real u, real v, real w,sval ul, sval vl, sval wl, sval udegree, sval vdegree, sval wdegree,real* coeffs);
4372 
4374 void catmullRomSpline(real t, real* coeffs);
4375 
4387 real mat4Det(real a, real b, real c, real d, real e, real f, real g, real h, real i, real j, real k, real l, real m, real n, real o, real p);
4388 
4390 bool pointInTet(vec3 pt, vec3 n1, vec3 n2, vec3 n3, vec3 n4);
4391 
4393 bool pointInHex(vec3 pt, vec3 n1, vec3 n2, vec3 n3, vec3 n4, vec3 n5, vec3 n6, vec3 n7, vec3 n8);
4394 
4396 inline float calculateTetVolume(vec3 a, vec3 b, vec3 c, vec3 d)
4397 {
4398  return -mat4Det(a.x(),b.x(),c.x(),d.x(),a.y(),b.y(),c.y(),d.y(),a.z(),b.z(),c.z(),d.z(),1,1,1,1)/6.0;
4399 }
4400 
4422 vec3 pointSearchLinTet(vec3 pt, vec3 n1, vec3 n2, vec3 n3, vec3 n4);
4423 
4429 vec3 pointSearchLinHex(vec3 pt, vec3 n1, vec3 n2, vec3 n3, vec3 n4, vec3 n5, vec3 n6, vec3 n7, vec3 n8);
4430 
4431 template<typename T>
4432 void cubicInterpMatrices(real t,const Matrix<T>* v1,const Matrix<T>* v2,const Matrix<T>* m1,const Matrix<T>* m2,const Matrix<T>* result)
4433 {
4434  sval rows=_min(v1->n(),_min(v2->n(),_min(m1->n(),_min(m2->n(),result->n()))));
4435  sval cols=_min(v1->m(),_min(v2->m(),_min(m1->m(),_min(m2->m(),result->m()))));
4436  real coeffs[4];
4437  catmullRomSpline(t,coeffs);
4438 
4439  for(sval i=0;i<rows;i++)
4440  for(sval j=0;j<cols;j++){
4441  T a=coeffs[0]*v1->at(i,j);
4442  T b=coeffs[1]*v2->at(i,j);
4443  T c=coeffs[2]*m1->at(i,j);
4444  T d=coeffs[3]*m2->at(i,j);
4445  result->at(i,j)=a+b+c+d;
4446  }
4447 }
4448 
4450 template<typename T>
4451 quadruple<int,int,int,int> calculateBoundSquare(const Matrix<T>* const mat,const T& threshold)
4452 {
4453  int minx=-1,maxx=-1,miny=-1,maxy=-1;
4454 
4455  sval rows=mat->n();
4456  sval cols=mat->m();
4457 
4458  for(sval i=0;i<rows && miny<0;i++)
4459  for(sval j=0;j<cols;j++)
4460  if(mat->at(i,j)>threshold){
4461  miny=int(i);
4462  break;
4463  }
4464 
4465  for(sval i=rows;i>0 && maxy<0;i--)
4466  for(sval j=0;j<cols;j++)
4467  if(mat->at(i-1,j)>threshold){
4468  maxy=int(i-1);
4469  break;
4470  }
4471 
4472  for(sval j=0;j<cols && minx<0;j++)
4473  for(sval i=0;i<rows;i++)
4474  if(mat->at(i,j)>threshold){
4475  minx=int(j);
4476  break;
4477  }
4478 
4479  for(sval j=cols;j>0 && maxx<0;j--)
4480  for(sval i=0;i<rows;i++)
4481  if(mat->at(i,j-1)>threshold){
4482  maxx=int(j-1);
4483  break;
4484  }
4485 
4486  return quadruple<int,int,int,int>(minx,miny,maxx,maxy);
4487 }
4488 
4489 template<typename T>
4490 sval countValuesInRange(const Matrix<T> *mat, const T& minv, const T& maxv)
4491 {
4492  sval count=0, rows=mat->n(), cols=mat->m();
4493 
4494  for(sval i=0;i<rows;i++)
4495  for(sval j=0;j<cols;j++){
4496  sval val=mat->at(i,j);
4497  if(val>=minv && val<=maxv)
4498  count++;
4499  }
4500 
4501  return count;
4502 }
4503 
4504 template<typename T>
4505 std::vector<vec3> findBoundaryPoints(const Matrix<T>* mat, const T& threshold)
4506 {
4507  std::vector<vec3> result;
4508 
4509  sval rows=mat->n();
4510  sval cols=mat->m();
4511 
4512  for(sval i=0;i<rows;i++)
4513  for(sval j=0;j<cols;j++){
4514  T val=mat->atc(i,j);
4515  if(val<threshold)
4516  continue;
4517 
4518  bool allInternal=true;
4519  for(sval n=_max<sval>(0,i-1);allInternal && n<_min(rows,i+1);n++)
4520  for(sval m=_max<sval>(0,j-1);allInternal && m<_min(cols,j+1);m++)
4521  if(n!=i || m!=j)
4522  allInternal=allInternal && mat->getAt(n,m)>=threshold;
4523 
4524  if(!allInternal)
4525  result.push_back(vec3(i,j,0));
4526  }
4527 
4528  return result;
4529 }
4530 
4531 template<typename T>
4532 T sumMatrix(const Matrix<T> *mat)
4533 {
4534  T result=T();
4535  sval rows=mat->n();
4536  sval cols=mat->m();
4537 
4538  for(sval i=0;i<rows;i++)
4539  for(sval j=0;j<cols;j++)
4540  result+=mat->atc(i,j);
4541 
4542  return result;
4543 }
4544 
4545 template<typename T>
4546 std::pair<T,T> minmaxMatrix(const Matrix<T>* mat) throw(ValueException)
4547 {
4548  sval rows=mat->n();
4549  sval cols=mat->m();
4550 
4551  if(rows==0)
4552  throw ValueException("mat","Matrix must be non-empty");
4553 
4554  std::pair<T,T> result(mat->atc(0,0),mat->atc(0,0));
4555 
4556  for(sval i=0;i<rows;i++)
4557  for(sval j=0;j<cols;j++){
4558  T val=mat->atc(i,j);
4559  if(val<result.first)
4560  result.first=val;
4561  else if(val>result.second)
4562  result.second=val;
4563  }
4564 
4565  return result;
4566 }
4567 
4568 template<typename T>
4569 T bilerpMatrix(const Matrix<T> *mat,real x, real y) throw(ValueException)
4570 {
4571  if(mat->n()==0)
4572  throw ValueException("mat","Matrix must be non-empty");
4573 
4574  if(x<0.0 || x>1.0 || y<0.0 || y>1.0)
4575  return T();
4576 
4577  x*=mat->m()-1;
4578  y*=mat->n()-1;
4579 
4580  sval sx=(sval)floor(x);
4581  sval sy=(sval)floor(y);
4582 
4583  real dx=x-sx;
4584  real dy=y-sy;
4585  real dx1=1.0-dx;
4586  real dy1=1.0-dy;
4587 
4588  return dx*(dy*mat->atc(sy+1,sx+1)+dy1*mat->atc(sy,sx+1)) + dx1*(dy*mat->atc(sy+1,sx)+dy1*mat->atc(sy,sx));
4589 }
4590 
4598 template<typename T>
4599 T trilerpMatrices(const Matrix<T>* mat1, const Matrix<T>* mat2, vec3 v1, vec3 v2) throw(ValueException)
4600 {
4601  T val1=bilerpMatrix<T>(mat1,v1.x(),v1.y());
4602  T val2=bilerpMatrix<T>(mat2,v2.x(),v2.y());
4603 
4604  real absz=fabs(v1.z());
4605  real lerpval=lerpXi<real>(absz,0,absz+fabs(v2.z()));
4606 
4607  return lerp(lerpval,val1,val2);
4608 }
4609 
4615 inline vec3 getPlaneXi(const vec3& pos, const vec3& planepos, const rotator& orientinv, const vec3& dimvec)
4616 {
4617  return (orientinv*(pos-planepos))/vec3(dimvec.x(),dimvec.y(),1);
4618 }
4619 
4624 void interpolateImageStack(const std::vector<RealMatrix*>& stack,const transform& stacktransinv,RealMatrix *out,const transform& outtrans);
4625 
4629 real getImageStackValue(const std::vector<RealMatrix*>& stack,const vec3& pos);
4630 
4631 void calculateImageHistogram(const RealMatrix* img, RealMatrix* hist, i32 minv);
4632 
4638 vec3* calculateTriNorms(vec3* nodes, sval numnodes, indexval* inds, sval numinds);
4639 
4640 } // namespace RenderTypes
4641 #endif // RENDERTYPES_H
static void parse(const char *line, sval numvals, T *list)
Definition: RenderTypes.h:4238
See CallbackVertexBuffer, the same concept applies here with a buffer accepting functions defined in ...
Definition: RenderTypes.h:3131
metamap::const_iterator citer
Definition: RenderTypes.h:1499
metamap _meta
Definition: RenderTypes.h:1501
sval calculateHexValueIntersects(real val, const real *vals, intersect *results)
Definition: RenderTypes.cpp:586
virtual bool usesDepthWrite() const
Definition: RenderTypes.h:2953
std::string _type
Definition: RenderTypes.h:1610
void stopTiming()
Definition: RenderTypes.h:330
virtual std::string getGlyphName() const
Definition: RenderTypes.h:3990
MatrixIndexBuffer(IndexMatrix *indices, IndexMatrix *extinds=NULL)
Create the buffer from these matrices. The caller is responsible for deleting these when appropriate...
Definition: RenderTypes.h:3239
void readBinaryFile(const char *filename, size_t offset)
Read a binary file of data into this matrix starting from byte `offset&#39;.
Definition: RenderTypes.h:1989
virtual sval getWidth() const
Definition: RenderTypes.h:3831
virtual vec3 getNormal(int i) const
Returns the i&#39;th normal, i<numVertices()
Definition: RenderTypes.h:3064
Matrix(const char *name, const char *type, sval n, sval m=1, bool isShared=false)
Constructs a matrix named `name&#39; with type `type&#39; of `n&#39; rows and `m&#39; columns, local if `isShared&#39; is...
Definition: RenderTypes.h:1634
virtual void setLookAt(const vec3 &v)
Definition: RenderTypes.h:3810
void add(const R &r, sval minrow=0, sval mincol=0, sval maxrow=-1, sval maxcol=-1)
Add `r&#39; to every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in the matrix.
Definition: RenderTypes.h:1844
void clear()
Definition: RenderTypes.h:891
bool lock(real timeout=0.0)
Definition: RenderTypes.h:412
bool hasMetaKey(const char *key) const
Returns true if a key-value pair with the given key is present.
Definition: RenderTypes.h:1505
real x() const
Definition: RenderTypes.h:634
virtual void setEmissive(const color &c)
Definition: RenderTypes.h:2974
virtual u8 * getData()
Get a pointer to the internal data buffer.
Definition: RenderTypes.h:3755
static vec3 negInfinity()
Definition: RenderTypes.h:860
DataSet(const char *name)
Definition: RenderTypes.h:2260
virtual bool hasNormal() const
Returns true if the buffer contains normal data.
Definition: RenderTypes.h:3124
Definition: RenderTypes.h:476
real z(real v)
Definition: RenderTypes.h:640
std::vector< std::string > fieldnames
Definition: RenderTypes.h:2257
Definition: RenderTypes.h:1584
virtual void setAlphaCurve(const Vec3Matrix *pts)
Definition: RenderTypes.h:2802
rotator conjugate() const
Definition: RenderTypes.h:1268
virtual sval numRibbons() const
Definition: RenderTypes.h:4003
color(float r=1.0f, float g=1.0f, float b=1.0f, float a=1.0f)
Definition: RenderTypes.h:551
bool isLinear
Definition: RenderTypes.h:2613
T _max(const T &a, const T &b)
Definition: RenderTypes.h:115
vec3 pos
Definition: RenderTypes.h:3279
virtual std::vector< std::string > getIndexNames() const
Lists the names of index matrices stored.
Definition: RenderTypes.h:2270
virtual sval numIndices() const
Returns the number of index sets.
Definition: RenderTypes.h:3263
std::vector< std::string > indexnames
Definition: RenderTypes.h:2256
virtual void setDiffuse(const color &c)
Sets the diffuse color, if usesInternalAlpha() returns true the alpha value will be set the internal ...
Definition: RenderTypes.h:2971
virtual void setSpecular(const color &c)
Set the specular color to reflect.
Definition: RenderTypes.h:3031
virtual void setLanguage(const std::string &lang)
Set the language for the source of the program, eg. cg.
Definition: RenderTypes.h:2338
real m10
Definition: RenderTypes.h:878
MemException(const MemException &op)
Definition: RenderTypes.h:1425
virtual std::string encode(const std::string &format)
Encode the image data as the byte stream for a file, the format of which is given by `format&#39; (eg...
Definition: RenderTypes.h:3757
vec3 pointSearchLinHex(vec3 pt, vec3 n1, vec3 n2, vec3 n3, vec3 n4, vec3 n5, vec3 n6, vec3 n7, vec3 n8)
Definition: RenderTypes.cpp:481
virtual bool usesLighting() const
Definition: RenderTypes.h:2950
virtual ~IndexException()
Definition: RenderTypes.h:1406
Definition: RenderTypes.h:475
sval m() const
Get the number of columns.
Definition: RenderTypes.h:1748
virtual void setScale(const vec3 &v)
Set the figure&#39;s scale values.
Definition: RenderTypes.h:3860
Partial templates for converting a line of text into a list of primitives/vec3.
Definition: RenderTypes.h:4236
int sortTupleThirdCB(const void *v1, const void *v2)
Definition: RenderTypes.h:162
virtual void setCameraAlign(bool align)
Definition: RenderTypes.h:4030
vec3 planeNorm(const vec3 &v2, const vec3 &v3) const
Returns the normal of a plane defined by `this&#39;, `v2&#39;, and `v3&#39; winding clockwise.
Definition: RenderTypes.h:773
ifunc indexfunc
Definition: RenderTypes.h:3138
real m23
Definition: RenderTypes.h:878
virtual void useDepthCheck(bool use)
Definition: RenderTypes.h:2989
virtual ~GlyphFigure()
Definition: RenderTypes.h:3985
Definition: RenderTypes.h:4017
virtual BlendMode getBlendMode() const
Definition: RenderTypes.h:2947
Mutex * parent
Definition: RenderTypes.h:376
Simple mutex type allowing locking and attempted locking with timeout.
Definition: RenderTypes.h:368
virtual bool hasError() const
Returns true if the source code given for the program has failed to parse.
Definition: RenderTypes.h:2344
virtual void setVertFOV(real rads)
Definition: RenderTypes.h:3818
virtual void setFarClip(real dist)
Definition: RenderTypes.h:3817
static const char * platformID
Definition: RenderTypes.h:95
std::string msg
Definition: RenderTypes.h:1435
Texture()
Definition: RenderTypes.h:2310
PositionQueue()
Definition: RenderTypes.h:2366
float g(float val)
Definition: RenderTypes.h:566
virtual void setBillboardDir(indexval index, const vec3 &dir)
Definition: RenderTypes.h:3952
virtual void fillData(const VertexBuffer *vb, const IndexBuffer *ib, bool deferFill=false, bool doubleSided=false)
Definition: RenderTypes.h:3899
IndexMatrix * extinds
Definition: RenderTypes.h:3234
bool deleteMatrices
Definition: RenderTypes.h:3166
realpair intersectsSphere(const vec3 &center, real rad) const
Definition: RenderTypes.h:3383
static R op(const LH &lh, const RH &rh)
Definition: RenderTypes.h:1587
virtual void addAlphaCtrl(vec3 v)
Definition: RenderTypes.h:2784
transform(const transform &t)
Definition: RenderTypes.h:3582
vec3 inv() const
Return a vector with each component of `this&#39; inverted or 0 if already 0.
Definition: RenderTypes.h:657
Definition: RenderTypes.h:444
virtual void setCtrlPoint(const T &t, indexval index)
Definition: RenderTypes.h:2462
virtual void setGPUProgram(const std::string &name, ProgramType pt)
Definition: RenderTypes.h:3000
T get(indexval index) const
Definition: RenderTypes.h:2392
static const char * RenderParamGroup
Definition: RenderTypes.h:97
Definition: RenderTypes.h:4101
virtual void setTexture(const char *name)
Definition: RenderTypes.h:2995
virtual sval getIndex(int i, int w) const
Returns the w&#39;th value of index set i.
Definition: RenderTypes.h:3149
color(const color &c)
Definition: RenderTypes.h:554
bool pointInTet(vec3 pt, vec3 n1, vec3 n2, vec3 n3, vec3 n4)
Returns true if `pt&#39; is in the tet (n1,n2,n3,n4).
Definition: RenderTypes.cpp:435
virtual real getVertFOV() const
Definition: RenderTypes.h:3826
Definition: RenderTypes.h:494
real determinant() const
Definition: RenderTypes.h:930
virtual vec3 getUVWCoord(int i) const
Returns the i&#39;th UVW texture coord, i<numVertices()
Definition: RenderTypes.h:3068
static T swap(T t)
Definition: RenderTypes.h:1591
virtual void fillColor(const RealMatrix *mat, indexval depth, real minval=0.0, real maxval=1.0, const Material *colormat=NULL, const RealMatrix *alphamat=NULL, bool mulAlpha=true)
Definition: RenderTypes.h:2323
virtual void fillColorMatrix(ColorMatrix *mat)
Transfer the image data into the given matrix.
Definition: RenderTypes.h:3761
transform(const vec3 &trans=vec3(), const vec3 &scale=vec3(1.0), const rotator &rot=rotator(), bool isInv=false)
Define a transform with translation and scale vectors and a rotator, default values define the identi...
Definition: RenderTypes.h:3578
rotator(const vec3 &from, const vec3 &to)
Defines the rotation which transforms normalized vectors `from&#39; to `to&#39; rotating about their cross pr...
Definition: RenderTypes.h:1023
Represents a texture loaded into memory and available to the graphis hardware.
Definition: RenderTypes.h:2307
real _x
Definition: RenderTypes.h:996
virtual void setDirectional()
Make this a directional light, illuminating all scene objects in the set direction.
Definition: RenderTypes.h:3034
#define destroy_mutex
Definition: RenderTypes.h:364
#define _HASH(h, v, s)
Definition: RenderTypes.h:86
void basis_Hex1NL(real xi0, real xi1, real xi2, real *coeffs)
Linear Nodal Lagrange hexahedron basis function, fills in `coeffs&#39; for the given xi value...
Definition: RenderTypes.cpp:308
virtual void setPointAttenuation(bool enabled, real constant=0.0f, real linear=1.0f, real quad=0.0f)
Sets point attenuation, the given real values are constants the attenuation equation.
Definition: RenderTypes.h:2983
static vec3 swap(vec3 t)
Definition: RenderTypes.h:1594
virtual rotator getRotation(bool isDerived=false) const
Get the figure&#39;s rotation.
Definition: RenderTypes.h:3878
void bswap(T &a, T &b)
Definition: RenderTypes.h:295
virtual bool isVisible() const
Returns true if this light is actively illuminating the scene.
Definition: RenderTypes.h:3049
static const real realInf
Definition: RenderTypes.h:112
virtual void fillBlack()
Definition: RenderTypes.h:2320
virtual void updateSpectrum()
Definition: RenderTypes.h:2727
virtual real getAlpha() const
Definition: RenderTypes.h:3965
~Locker()
Definition: RenderTypes.h:382
Definition: RenderTypes.h:480
virtual DataSet * clone(const char *name, bool cloneNodes=false)
Definition: RenderTypes.h:2264
void meta(const MetaType *m)
Copy all the metadata from `m&#39; to `this&#39;.
Definition: RenderTypes.h:1547
virtual Config * getConfig() const
Returns the Config object used to define properties for the scene.
Definition: RenderTypes.h:4152
virtual ~PositionQueue()
Definition: RenderTypes.h:2367
mat4 toMatrix() const
Definition: RenderTypes.h:3725
virtual ~RenderException()
Definition: RenderTypes.h:1447
Definition: RenderTypes.h:1381
real planeDist(const vec3 &planept, const vec3 &planenorm) const
Returns the distance from `this&#39; to a plane defined by a point on the plane and the plane normal (pos...
Definition: RenderTypes.h:783
Definition: RenderTypes.h:1432
std::string msg
Definition: RenderTypes.h:1414
Matrix(const char *name, sval n, sval m=1, bool isShared=false)
Constructs a matrix named `name&#39; of `n&#39; rows and `m&#39; columns, local if `isShared&#39; is false and shared...
Definition: RenderTypes.h:1626
static real conv(const char *str)
Definition: RenderTypes.h:4232
virtual void setPosition(const vec3 &v)
Set the figure&#39;s position in world space.
Definition: RenderTypes.h:3858
Config()
Definition: RenderTypes.h:4060
T trilerpMatrices(const Matrix< T > *mat1, const Matrix< T > *mat2, vec3 v1, vec3 v2)
Definition: RenderTypes.h:4599
void clear()
Definition: RenderTypes.h:2390
bool equalsEpsilon(real v1, real v2)
Returns true if `v1&#39; and `v2&#39; are within &#39;dEPSILON&#39; of one another.
Definition: RenderTypes.h:173
def first(iterable, default=None)
Definition: Utils.py:2081
void copyFrom(const PositionQueue< T > *queue)
Definition: RenderTypes.h:2369
virtual bool hasIndexSet(const char *name) const
Returns true if an index matrix of the given name is stored.
Definition: RenderTypes.h:2279
virtual ~GPUProgram()
Definition: RenderTypes.h:2330
rotator(real x, real y, real z, real w)
Defines a rotator by the given quaternion values.
Definition: RenderTypes.h:1017
u32 sval
Definition: RenderTypes.h:107
mat4()
Definition: RenderTypes.h:886
sval size() const
Definition: RenderTypes.h:2388
virtual void rotate(const rotator &r)
Definition: RenderTypes.h:3813
virtual real getSpaceWidth() const
Definition: RenderTypes.h:4039
virtual vec3 getUVWCoord(int i) const
Returns the i&#39;th UVW texture coord, i<numVertices()
Definition: RenderTypes.h:3121
static const char * parentPIDVar
Definition: RenderTypes.h:96
virtual std::string getName() const
Definition: RenderTypes.h:2332
virtual ~TextureVolumeFigure()
Definition: RenderTypes.h:3959
virtual bool usesPointSprites() const
Definition: RenderTypes.h:2957
virtual void addGlyphMesh(const std::string &name, const Vec3Matrix *nodes, const Vec3Matrix *norms, const IndexMatrix *inds)
Definition: RenderTypes.h:3991
Definition: RenderTypes.h:275
sval numverts
Definition: RenderTypes.h:3165
const char * getSharedName() const
Definition: RenderTypes.h:1678
void reserveRows(sval num)
Ensure that at least `num&#39; rows are reserved in memory, throws exception if shared.
Definition: RenderTypes.h:1957
color unitClamp()
Definition: RenderTypes.h:595
const char * getName() const
Definition: RenderTypes.h:2262
void initSharedDir(const std::string &path)
Definition: RenderTypes.cpp:27
real _w
Definition: RenderTypes.h:996
float b() const
Definition: RenderTypes.h:562
FigureType
Defines the figure types which the Figure class and subclasses are capable of representing.
Definition: RenderTypes.h:441
virtual color getDiffuse() const
Definition: RenderTypes.h:2938
#define MAXSHMNAMLEN
Definition: RenderTypes.h:75
virtual color getSpectrumValue(indexval index) const
Get the color of the spectrum value at the given index in the list.
Definition: RenderTypes.h:2743
Definition: RenderTypes.h:447
T sumMatrix(const Matrix< T > *mat)
Definition: RenderTypes.h:4532
std::string msg
Definition: RenderTypes.h:1385
virtual const char * getName()
Get the figure&#39;s name.
Definition: RenderTypes.h:3856
MemException(const std::string m, int err)
Definition: RenderTypes.h:1418
virtual void setPointSize(real min, real max)
Set the minimum and maximum point size for attenuated points.
Definition: RenderTypes.h:2979
bool onPlane(const vec3 &planept, const vec3 &planenorm) const
Returns true if the ray is on the plane defined by the position `planept&#39; and normal `planenorm&#39;...
Definition: RenderTypes.h:3326
wfunc widthfunc
Definition: RenderTypes.h:3137
virtual void setPointSizeAbs(real size)
Set the absolute point size.
Definition: RenderTypes.h:2981
virtual void setText(const std::string &text)
Definition: RenderTypes.h:4021
void append(const T &t, sval m=0)
Append `t&#39; to a new row, placing it in column `m&#39;.
Definition: RenderTypes.h:1971
virtual const char * getName() const
Definition: RenderTypes.h:2707
Definition: RenderTypes.h:3850
virtual color getDefaultColor() const
Definition: RenderTypes.h:2916
Definition: RenderTypes.h:3982
real m03
Definition: RenderTypes.h:878
colfunc colorfunc
Definition: RenderTypes.h:3109
virtual void setRenderQueue(sval queue)
Get the render queue of this figure; queues set rendering order such that figures in lower queues are...
Definition: RenderTypes.h:3917
virtual real getAlpha() const
Get the alpha (transparency) value, 1.0 is opaque and 0.0 is invisible, -1.0 if alpha shouldn&#39;t be us...
Definition: RenderTypes.h:2914
#define dEPSILON
Definition: RenderTypes.h:84
static vec3 X()
Returns the X-axis unit vector.
Definition: RenderTypes.h:863
Definition: RenderTypes.h:503
Matrix< vec3 > Vec3Matrix
Definition: RenderTypes.h:2239
virtual void setHAlign(HAlignType align)
Definition: RenderTypes.h:4026
std::string toString()
Definition: RenderTypes.h:4069
virtual real getSpectrumPos(indexval index) const
Get the position of the spectrum value at the given index in the list.
Definition: RenderTypes.h:2737
void clear()
Definition: RenderTypes.h:2455
virtual void setBGColor(const color &c)
Definition: RenderTypes.h:3819
int sortTupleFourthCB(const void *v1, const void *v2)
Definition: RenderTypes.h:167
vec3 cross(const vec3 &v) const
Return the cross product of `this&#39; and `v&#39;.
Definition: RenderTypes.h:661
Implementation of a IndexBuffer which uses matrices for storage, like MatrixVertexBuffer.
Definition: RenderTypes.h:3231
void setRotation(const rotator &r)
Definition: RenderTypes.h:3598
virtual sval getHeight() const
Get the image height.
Definition: RenderTypes.h:3749
bool loopOnce()
Definition: RenderTypes.h:340
i32 hash() const
Definition: RenderTypes.h:828
virtual void initializeResources()
Onces all resource directories are added, initialize the internal resource system.
Definition: RenderTypes.h:4125
S second
Definition: RenderTypes.h:266
Matrix< real > RealMatrix
Definition: RenderTypes.h:2238
float r(float val)
Definition: RenderTypes.h:565
rotator(real yaw, real pitch, real roll)
Defines a rotation from Euler angles: yaw = Z-axis rotation, pitch = X-axis rotation, roll = Y-axis rotation.
Definition: RenderTypes.h:1036
virtual real getWidth() const
Definition: RenderTypes.h:3942
vec3(real val=0)
Construct a vector by setting all components to `val&#39;.
Definition: RenderTypes.h:628
Definition: RenderTypes.h:464
bool loopOnce()
Definition: RenderTypes.h:384
virtual void setTexture(const Texture *tex)
Definition: RenderTypes.h:2996
virtual void fillColor(color col)
Definition: RenderTypes.h:2321
Definition: RenderTypes.h:3772
void setType(const char *type)
Definition: RenderTypes.h:1682
bool alwaysHighQuality
Definition: RenderTypes.h:4104
Definition: RenderTypes.h:501
virtual void setPoint()
Make this a point light, illuminating all objects within range as defined by the attenuation settings...
Definition: RenderTypes.h:3037
Locker(Mutex *p, real timeout=0.0)
Definition: RenderTypes.h:380
CallbackIndexBuffer(Ctx context, sval numindices, wfunc widthfunc, ifunc indexfunc)
Definition: RenderTypes.h:3143
rotator getRotation() const
Definition: RenderTypes.h:3592
virtual vec3 getVertex(int i) const
Returns the i&#39;th vertex, i<numVertices()
Definition: RenderTypes.h:3118
virtual color getDefaultColor() const
Definition: RenderTypes.h:2723
real y() const
Definition: RenderTypes.h:635
float alpha
Definition: RenderTypes.h:2901
virtual std::string getLanguage() const
Definition: RenderTypes.h:2335
virtual bool usesInternalAlpha() const
Returns true if the internal alpha value is to be applied to colors, otherwise the alpha components o...
Definition: RenderTypes.h:2927
void set(indexval index, real pos, T value)
Definition: RenderTypes.h:2397
void divm(const Matrix< R > &mat, sval minrow=0, sval mincol=0, sval maxrow=-1, sval maxcol=-1)
Divide every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in the matrix by the same in `mat&#39;...
Definition: RenderTypes.h:1886
void deserializeMeta(const std::string &s)
Break a string generated by the above back into name-value pairs and store in `this&#39;.
Definition: RenderTypes.h:1564
metamap::iterator iter
Definition: RenderTypes.h:1498
virtual real getTextHeight() const
Definition: RenderTypes.h:4038
rotator()
Default no-op.
Definition: RenderTypes.h:1000
virtual void setCtrlPoints(const Matrix< T > *pts)
Definition: RenderTypes.h:2467
Ray()
Definition: RenderTypes.h:3283
void readTextFile(const char *filename, sval numHeaders)
Read a text file of data into this matrix which has with `numHeaders&#39; values in the header line...
Definition: RenderTypes.h:1995
const T & atc(sval n, sval m=0) const
Definition: RenderTypes.h:1918
virtual void setOrtho(bool isOrtho)
Definition: RenderTypes.h:3822
virtual vec3 getPosition() const
Definition: RenderTypes.h:3790
real m01
Definition: RenderTypes.h:878
void setTranslation(const vec3 &v)
Definition: RenderTypes.h:3596
quadruple(const F &first, const S &second, const T &third, const U &fourth)
Definition: RenderTypes.h:284
mat4 inverse() const
Definition: RenderTypes.h:948
bool isZero() const
Returns true if the length of the vector is within dEPSILON of 0.
Definition: RenderTypes.h:698
vec3 toPolar() const
Returns an equivalent vector in polar coordinates, assuming `this&#39; was in cartesian coordinates...
Definition: RenderTypes.h:686
Represents a Red-Green-Blue-Alpha color with float channels. Note the lack of virtual members implies...
Definition: RenderTypes.h:529
virtual void setWireframe(bool isWireframe)
Definition: RenderTypes.h:3823
std::pair< real, T > ListValue
Definition: RenderTypes.h:2362
const char * getName() const
Definition: RenderTypes.h:1677
#define trylock_mutex(m)
Definition: RenderTypes.h:361
TextureFormat
Definition: RenderTypes.h:473
virtual void copyTo(Material *mat, bool copyTex=false, bool copySpec=false, bool copyProgs=false) const
Copy this material&#39;s settings to `mat&#39;.
Definition: RenderTypes.h:2911
real m12
Definition: RenderTypes.h:878
virtual vec3 getVertex(int i) const
Returns the i&#39;th vertex, i<numVertices()
Definition: RenderTypes.h:3219
int compV(const void *t1, const void *t2)
Definition: RenderTypes.h:147
virtual void setIndexNames(std::vector< std::string > names)
Sets the internal list of index names, should be used to update name list whenever setIndexSet is cal...
Definition: RenderTypes.h:2273
triple()
Definition: RenderTypes.h:269
bool renderHighQuality
Definition: RenderTypes.h:4103
Definition: RenderTypes.h:3569
vec3 getTranslation() const
Definition: RenderTypes.h:3590
vec3 * calculateTriNorms(vec3 *nodes, sval numnodes, indexval *inds, sval numinds)
Definition: RenderTypes.cpp:664
static indexval swap(indexval t)
Definition: RenderTypes.h:1593
virtual real getAlpha() const
Definition: RenderTypes.h:2725
Vec3Matrix * vecs
Definition: RenderTypes.h:3162
static rgba interpolate(real val, rgba left, rgba right)
Fast linear interpolation between rgba values.
Definition: RenderTypes.h:535
void setBuff(float *v) const
Definition: RenderTypes.h:570
virtual void setColor(const color &col)
Definition: RenderTypes.h:4023
virtual bool hasNormal() const
Returns true if the buffer contains normal data.
Definition: RenderTypes.h:3225
virtual bool hasColor() const
Returns true if the buffer contains color data.
Definition: RenderTypes.h:3226
virtual sval numIndices() const
Returns the number of index sets.
Definition: RenderTypes.h:3087
std::string msg
Definition: RenderTypes.h:1455
ControlCurve()
Definition: RenderTypes.h:2445
virtual ~ValueException()
Definition: RenderTypes.h:1469
real m22
Definition: RenderTypes.h:878
IndexException(const IndexException &ind)
Definition: RenderTypes.h:1401
virtual sval getRenderQueue() const
Get the figure&#39;s render queue.
Definition: RenderTypes.h:3919
static R op(const LH &lh, const RH &rh)
Definition: RenderTypes.h:1584
virtual void setZUp()
Definition: RenderTypes.h:3812
size_t val
Definition: RenderTypes.h:1386
void calculateImageHistogram(const RealMatrix *img, RealMatrix *hist, i32 minv)
Definition: RenderTypes.cpp:652
vec3 pointSearchLinTet(vec3 pt, vec3 n1, vec3 n2, vec3 n3, vec3 n4)
Definition: RenderTypes.cpp:446
virtual indexval getSpectrumIndex(real pos, color value) const
Get the index of the spectrum value with the given position and color, returns numSpectrumValues() if...
Definition: RenderTypes.h:2749
MemException(const std::string msg)
Definition: RenderTypes.h:1416
virtual void setNumPlanes(sval num)
Definition: RenderTypes.h:3961
int compT(const T &t1, const T &t2)
Definition: RenderTypes.h:137
real angleTo(const vec3 &v) const
Returns the angle between `this&#39; and `v&#39;.
Definition: RenderTypes.h:754
real lineDist(vec3 p1, vec3 p2) const
Returns the cylindrical distance from `this&#39; to the line segment `p1&#39;->`p2&#39;, or -1 if p1==p2 or `this...
Definition: RenderTypes.h:809
virtual void removeNode(sval ribbon)
Definition: RenderTypes.h:4010
void convertRealStreamToRealMatrix(const char *stream, RealMatrix *mat)
Definition: RenderTypes.cpp:275
Matrix< ListValue > vals
Definition: RenderTypes.h:2363
std::vector< vec3 > findBoundaryPoints(const Matrix< T > *mat, const T &threshold)
Definition: RenderTypes.h:4505
Definition: RenderTypes.h:496
real m13
Definition: RenderTypes.h:878
virtual std::string getParameter(const std::string &param) const
Definition: RenderTypes.h:2350
real atX(real x, real threshold=0.0001) const
Definition: RenderTypes.h:2655
This subtype of Figure represents a set of billboards, squares with textures in space which are orien...
Definition: RenderTypes.h:3935
Definition: RenderTypes.cpp:23
void removeRow(sval n)
Definition: RenderTypes.h:1977
virtual Vec3Matrix * getNodes() const
Definition: RenderTypes.h:2266
triple(const triple< F, S, T > &t)
Definition: RenderTypes.h:271
virtual real getNearClip() const
Definition: RenderTypes.h:3828
void convertFloatStreamToRealMatrix(const char *stream, RealMatrix *mat)
Definition: RenderTypes.cpp:270
std::string getPPIDStr()
Definition: RenderTypes.h:208
virtual void setDataField(RealMatrix *field, const char *alias=NULL)
Add a new field matrix to the data set using its internal name or the supplied name, replaces an existing stored matrix.
Definition: RenderTypes.h:2297
void unlinkShared(const std::string &name)
Definition: RenderTypes.cpp:62
vec3 planeProject(const vec3 &planept, const vec3 &planenorm) const
Returns the projection of `this&#39; on a plane defined by a point on the plane and the plane normal...
Definition: RenderTypes.h:786
Definition: RenderTypes.h:465
real _y
Definition: RenderTypes.h:996
rgba toRGBA() const
Convert this color to a 32-bit red-green-blue-alpha value suitable for certain renderer input values...
Definition: RenderTypes.h:573
void addm(const Matrix< R > &mat, sval minrow=0, sval mincol=0, sval maxrow=-1, sval maxcol=-1)
Add every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in `mat&#39; to the same cell in the matrix...
Definition: RenderTypes.h:1868
virtual bool hasUVWCoord() const
Returns true if the buffer contains texture coord data.
Definition: RenderTypes.h:3227
This type is used with the TIMING and TIMINGBLOCK macros to time routine calls and code blocks...
Definition: RenderTypes.h:306
void toMatrix(real *mat) const
Definition: RenderTypes.h:1327
real intersectsLineSeg(const vec3 &v1, const vec3 &v2) const
Returns 0 if `v1&#39; or `v2&#39; are the origin of this ray, a value t >= 0 if the ray intersects the line a...
Definition: RenderTypes.h:3438
float a() const
Definition: RenderTypes.h:563
virtual sval getHeight() const
Definition: RenderTypes.h:2316
virtual void setEntryPoint(const std::string main)
Definition: RenderTypes.h:2354
void chooseSharedName(int counter=0)
Determine a shared name based on the name stored in this matrix and the counter value, used to determine a system-wide unique name.
Definition: RenderTypes.h:2084
virtual vec3 getGlyphScale() const
Definition: RenderTypes.h:3988
sval getIndex(sval i) const
Definition: RenderTypes.h:3217
void scalarop(const R &r, sval minrow=0, sval mincol=0, sval maxrow=-1, sval maxcol=-1)
Apply the operation OpType::op to every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in the matri...
Definition: RenderTypes.h:1821
virtual void setViewport(real left=0.0f, real top=0.0f, real width=1.0f, real height=1.0f)
Definition: RenderTypes.h:3821
Definition: RenderTypes.h:449
Definition: RenderTypes.h:477
real m21
Definition: RenderTypes.h:878
Definition: RenderTypes.h:3160
void copyFrom(const Matrix< R > *r)
Copy the data bitwise from `r&#39;, the number of bytes copied is the minimum or either matrices&#39; size...
Definition: RenderTypes.h:1762
std::pair< real, real > realpair
Definition: RenderTypes.h:289
virtual TextureFormat getFormat() const
Definition: RenderTypes.h:2319
MatrixVertexBuffer(const VertexBuffer *buf)
Copy the data from `buf&#39; into internal matrices which this object is responsible for and will delete ...
Definition: RenderTypes.h:3180
virtual bool isTransparentColor() const
Returns true if the intenal alpha value is used, if it&#39;s <1.0, if vertex colors are not used...
Definition: RenderTypes.h:2967
Definition: RenderTypes.h:448
vec3(const vec3 &v)
Copy constructor.
Definition: RenderTypes.h:632
bool deleteMatrices
Definition: RenderTypes.h:3235
virtual sval getMaxNodes() const
Definition: RenderTypes.h:4006
Definition: RenderTypes.h:872
virtual bool setGPUParamColor(ProgramType pt, const std::string &name, color val)
Definition: RenderTypes.h:3006
virtual color interpolateColor(real pos) const
Definition: RenderTypes.h:2826
virtual std::string getProfiles() const
Definition: RenderTypes.h:2352
Definition: RenderTypes.h:487
float g() const
Definition: RenderTypes.h:561
real len() const
Definition: RenderTypes.h:1275
void storeBufftoBinaryFile(const char *filename, void *src, size_t srcsize, int *header, size_t headerlen)
Using mmap, copy the contents of `header&#39; and then `src&#39; into file `filename&#39;.
Definition: RenderTypes.cpp:150
realpair intersectsRay(const Ray &ray) const
Definition: RenderTypes.h:3405
virtual T getCtrlPoint(indexval index) const
Definition: RenderTypes.h:2465
F first
Definition: RenderTypes.h:265
real getYaw() const
Get the yaw angle (z-axis rotation in radians)
Definition: RenderTypes.h:1165
Represents a GPU program (vertex/fragment/geometry shader)
Definition: RenderTypes.h:2327
real m31
Definition: RenderTypes.h:878
bool inSphere(const vec3 &center, real radius) const
Returns true if the vector&#39;s distance to `center&#39; is less than or equal to `radius&#39;+`dEPSILON&#39;.
Definition: RenderTypes.h:718
void checkDimension(const char *name, sval dim) const
Definition: RenderTypes.h:2042
virtual RenderScene * getRenderScene()
Definition: RenderTypes.h:4207
VAlignType
Definition: RenderTypes.h:499
void setMsg()
Definition: RenderTypes.h:1388
void interpolateImageStack(const std::vector< RealMatrix *> &stack, const transform &stacktransinv, RealMatrix *out, const transform &outtrans)
Definition: RenderTypes.cpp:611
vec3 toCylindrical() const
Returns an equivalent vector in cylindrical coordinates, assuming `this&#39; was in cartesian coordinates...
Definition: RenderTypes.h:689
virtual bool usesFlatShading() const
Definition: RenderTypes.h:2951
virtual color getAmbient() const
Definition: RenderTypes.h:2937
T swapEndianN(T t)
Definition: RenderTypes.h:226
virtual std::string getEntryPoint() const
Definition: RenderTypes.h:2351
sval _n_actual
Definition: RenderTypes.h:1614
unsigned int u32
Definition: RenderTypes.h:103
bool getRenderHighQuality() const
Returns whether the next render operation will be in high quality mode.
Definition: RenderTypes.h:4167
void mul(const R &r, sval minrow=0, sval mincol=0, sval maxrow=-1, sval maxcol=-1)
Multiple every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in the matrix by `r&#39;.
Definition: RenderTypes.h:1856
Definition: RenderTypes.h:2438
virtual ~BBSetFigure()
Definition: RenderTypes.h:3938
rotator inverse() const
Returns a rotator representing the opposite rotation.
Definition: RenderTypes.h:1304
Definition: RenderTypes.h:478
rotator(const vec3 &axis, real rads)
Defines a rotation about the given axis.
Definition: RenderTypes.h:1010
int planeOrder(const vec3 &planenorm, const vec3 &v1, const vec3 &v2) const
Given a plane defined by this and `planenorm&#39;, returns the circular ordering of `v1&#39; and `v2&#39;...
Definition: RenderTypes.h:789
virtual ~Matrix()
Definition: RenderTypes.h:1661
virtual void useFlatShading(bool use)
Definition: RenderTypes.h:2988
vec3 fromCylindrical() const
Returns an equivalent vector in cartesian coordinates, assuming `this&#39; was in cylindrical coordinates...
Definition: RenderTypes.h:695
void setMatrixMinMax(Matrix< T > *mat, const V &minv, const V &maxv)
Definition: RenderTypes.h:4220
virtual bool isVisible() const
Returns the figure&#39;s visibility state.
Definition: RenderTypes.h:3904
virtual bool hasUVWCoord() const
Returns true if the buffer contains texture coord data.
Definition: RenderTypes.h:3126
virtual void setFont(const std::string &fontname)
Definition: RenderTypes.h:4022
virtual const char * what() const
Definition: RenderTypes.h:1429
Partial templates for converting strings to primitives/vec3.
Definition: RenderTypes.h:4231
virtual bool hasColor() const
Returns true if the buffer contains color data.
Definition: RenderTypes.h:3125
rotator(vec3 row1, vec3 col1, vec3 row2, vec3 col2)
Definition: RenderTypes.h:1096
void setLinear(bool b)
Definition: RenderTypes.h:2617
virtual bool hasAlpha() const
Definition: RenderTypes.h:2318
int cmp(const vec3 &v) const
Definition: RenderTypes.h:741
virtual vec3 getVertex(int i) const
Returns the i&#39;th vertex, i<numVertices()
Definition: RenderTypes.h:3062
rotator(const rotator &r)
Copy constructor.
Definition: RenderTypes.h:1004
void subm(const Matrix< R > &mat, sval minrow=0, sval mincol=0, sval maxrow=-1, sval maxcol=-1)
Subtract every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in `mat&#39; from the same cell in the ma...
Definition: RenderTypes.h:1874
Definition: RenderTypes.h:1606
rotator interpolate(real val, const rotator &r) const
Semi-linearly interpolates between `this&#39; and `r&#39;, this varies from a circular interpolation but is f...
Definition: RenderTypes.h:1312
virtual Texture * loadTextureFile(const char *name, const char *absFilename)
Load a texture of the given name from the image absolute path filename.
Definition: RenderTypes.h:4143
virtual ~Light()
Definition: RenderTypes.h:3019
Vec3Curve(bool isXFunc)
Definition: RenderTypes.h:2615
indexval find(real pos, const T &value) const
Definition: RenderTypes.h:2414
virtual void addResourceDir(const char *dir)
Add a directory to search for resources in.
Definition: RenderTypes.h:4122
bool signz
Definition: RenderTypes.h:3280
virtual void useVertexColor(bool use)
Definition: RenderTypes.h:2986
virtual color getSpecular() const
Definition: RenderTypes.h:2939
virtual void logMessage(const char *msg)
Log a message to the renderer log file.
Definition: RenderTypes.h:4155
This base type provides facilities for maintaining name-value metadata pairs.
Definition: RenderTypes.h:1493
void checkIndex(const char *name, sval val, sval maxval) const
Definition: RenderTypes.h:2030
void convertIntStreamToRealMatrix(const char *stream, RealMatrix *mat)
Definition: RenderTypes.cpp:265
std::string _name
Definition: RenderTypes.h:1609
Definition: RenderTypes.h:993
virtual vec3 getTexXiDir(vec3 pos) const
Definition: RenderTypes.h:3974
transform inverse() const
Get the inverse transform of `this&#39;, ie. for any transform t and vec3 v (t.inverse()*(t*v)) is a no-o...
Definition: RenderTypes.h:3680
virtual void setIndexSet(IndexMatrix *indices, const char *alias=NULL)
Add a new index matrix to the data set using its internal name or the supplied name, replaces an existing stored matrix.
Definition: RenderTypes.h:2282
real fround(real r)
Definition: RenderTypes.h:190
void storeBinaryFile(const char *filename, int *header, sval headerlen)
Store the header values `header&#39; and then this matrix&#39;s contents to the file.
Definition: RenderTypes.h:2002
virtual void setAlpha(real alpha)
Set the internal alpha value, resetting the diffuse and specular values expected a subtype to set the...
Definition: RenderTypes.h:2919
virtual void setDimension(real width, real height)
Definition: RenderTypes.h:3940
real distTo(const vec3 &v) const
Return the distance from `this&#39; to `v&#39;.
Definition: RenderTypes.h:671
virtual const char * getGPUProgram(ProgramType pt) const
Definition: RenderTypes.h:2959
void readBinaryFileToBuff(const char *filename, size_t offset, void *dest, size_t len)
Using mmap, copy the contents from file `filename&#39; into `dest&#39; starting `offset&#39; bytes from the begin...
Definition: RenderTypes.cpp:90
vec3 getDirection() const
Get the direction the ray is pointing.
Definition: RenderTypes.h:3300
std::pair< std::string, std::string > strpair
Definition: RenderTypes.h:4054
Definition: RenderTypes.h:453
virtual void setCtrlPoint(const vec3 &t, indexval index)
Definition: RenderTypes.h:2630
virtual Figure * createFigure(const char *name, const char *mat, FigureType type)
Create a figure of the given name, with material named by `mat&#39;, and type `type&#39;. ...
Definition: RenderTypes.h:4131
virtual void usePointSprites(bool useSprites)
Definition: RenderTypes.h:2994
virtual void setAlpha(real a)
Definition: RenderTypes.h:3964
CallbackVertexBuffer(Ctx context, sval numvertices, vecfunc vertfunc, vecfunc normalfunc=NULL, colfunc colorfunc=NULL, vecfunc uvwfunc=NULL)
Definition: RenderTypes.h:3114
virtual real getAspectRatio() const
Get the aspect ratio of the notional box this camera sees through and shall render to a target...
Definition: RenderTypes.h:3781
virtual void calculateDerivs()
Definition: RenderTypes.h:2476
virtual ~RibbonFigure()
Definition: RenderTypes.h:3997
virtual void setSourceCode(const std::string &code)
Set the source code for the program, this must be done in the main thread.
Definition: RenderTypes.h:2341
virtual real getPointSizeAbs() const
Definition: RenderTypes.h:2945
virtual bool isClampTexAddress() const
Definition: RenderTypes.h:2955
void basis_Tet1NL(real xi0, real xi1, real xi2, real *coeffs)
Linear Nodal Lagrange tetrahedron basis function, fills in `coeffs&#39; for the given xi value...
Definition: RenderTypes.cpp:300
vecfunc normalfunc
Definition: RenderTypes.h:3108
virtual void useInternalAlpha(bool val)
Set whether to use the internal alpha value or use those specified in the diffuse and specular color ...
Definition: RenderTypes.h:2930
virtual void useTexFiltering(bool use)
Definition: RenderTypes.h:2991
virtual void setFieldNames(std::vector< std::string > names)
Sets the internal list of field names, should be used to update name list whenever setDataField is ca...
Definition: RenderTypes.h:2288
void calculateTetValueIntersects(real val, real a, real b, real c, real d, real *results)
Definition: RenderTypes.cpp:576
Definition: RenderTypes.h:4199
virtual Camera * createCamera(const char *name, real left=0.0f, real top=0.0f, real width=1.0f, real height=1.0f)
Definition: RenderTypes.h:4116
void convertUIntStreamToRealMatrix(const char *stream, RealMatrix *mat)
Definition: RenderTypes.cpp:260
virtual ~MatrixIndexBuffer()
Definition: RenderTypes.h:3255
sval memSize() const
Get the total memory usage in bytes.
Definition: RenderTypes.h:1751
Definition: RenderTypes.h:461
virtual void setUpVector(const vec3 &v)
Definition: RenderTypes.h:3945
virtual sval getIndex(int i, int w) const
Returns the w&#39;th value of index set i.
Definition: RenderTypes.h:3091
bool isInverse() const
Definition: RenderTypes.h:3594
u32 indexval
Definition: RenderTypes.h:110
virtual u64 createWindow(int width, int height)
Definition: RenderTypes.h:4204
virtual void setSpectrumValue(sval index, real pos, color value)
Set the spectrum value at the given list index.
Definition: RenderTypes.h:2755
bool hasValue(const char *name) const
Definition: RenderTypes.h:4065
u32 rgba
Definition: RenderTypes.h:109
void normThis()
Definition: RenderTypes.h:1292
Definition: RenderTypes.h:489
T _min(const T &a, const T &b)
Definition: RenderTypes.h:114
virtual void fillColorMatrix(ColorMatrix *col, const RealMatrix *mat, bool useValAsAlpha=false)
Definition: RenderTypes.h:2868
quadruple()
Definition: RenderTypes.h:283
static vec3 Z()
Returns the Z-axis unit vector.
Definition: RenderTypes.h:869
Definition: RenderTypes.h:446
virtual real getPointSizeMin() const
Definition: RenderTypes.h:2943
virtual IndexMatrix * getIndexSet(const char *name) const
Get the index matrix of the given name, or NULL if not found.
Definition: RenderTypes.h:2276
virtual void setDirection(vec3 &v)
Set the direction to emit light at, only meaningful for directional and spot lights.
Definition: RenderTypes.h:3025
Definition: RenderTypes.h:445
bool doPrint
Definition: RenderTypes.h:311
virtual bool setGPUParamVec3(ProgramType pt, const std::string &name, vec3 val)
Definition: RenderTypes.h:3005
bool useAlpha
Definition: RenderTypes.h:2902
real w() const
Definition: RenderTypes.h:1146
T swapEndian64(T t)
Definition: RenderTypes.h:246
virtual ~RenderScene()
Definition: RenderTypes.h:4109
real getRoll() const
Get the roll angle (y-axis rotation in radians)
Definition: RenderTypes.h:1178
virtual void setDiffuse(const color &c)
Set the diffuse color to emit.
Definition: RenderTypes.h:3028
Definition: RenderTypes.h:3101
float a(float val)
Definition: RenderTypes.h:568
S second
Definition: RenderTypes.h:279
vec3 fromPolar() const
Returns an equivalent vector in cartesian coordinates, assuming `this&#39; was in polar coordinates...
Definition: RenderTypes.h:692
void setRenderHighQuality(bool val)
Set whether rendering should be done using high quality passes or not.
Definition: RenderTypes.h:4161
std::pair< T, T > minmaxMatrix(const Matrix< T > *mat)
Definition: RenderTypes.h:4546
vec3 lerp(real val, const vec3 &v) const
Linearly interpolate between `this&#39; and `v&#39;.
Definition: RenderTypes.h:823
virtual void setOverlay(bool isOverlay)
Set the overlay state of the figure, this doesn&#39;t actually change data but affects how the renderer t...
Definition: RenderTypes.h:3914
virtual sval numAlphaCtrls() const
Definition: RenderTypes.h:2774
Definition: RenderTypes.h:454
void setShared(bool val)
Definition: RenderTypes.h:1695
virtual void setGPUProgram(const GPUProgram *prog)
Definition: RenderTypes.h:3001
Ray(const Ray &r)
Definition: RenderTypes.h:3291
Definition: RenderTypes.h:3956
virtual std::string getText() const
Definition: RenderTypes.h:4032
virtual mat4 getProjMatrix() const
Definition: RenderTypes.h:3795
void ident()
Definition: RenderTypes.h:892
void setAxis(const vec3 &axis, real rads)
Set the rotator to represent a rotation of `rads&#39; radians around `axis&#39;.
Definition: RenderTypes.h:1114
virtual GPUProgram * createGPUProgram(const char *name, ProgramType ptype, const char *language)
Load a GPU program (shader) of the given name, type, and language (ie. Cg).
Definition: RenderTypes.h:4146
virtual void addCtrlPoint(const T &t)
Definition: RenderTypes.h:2461
T * dataPtr() const
Definition: RenderTypes.h:1666
Definition: RenderTypes.h:2898
std::string meta() const
Definition: RenderTypes.h:1516
sval countValuesInRange(const Matrix< T > *mat, const T &minv, const T &maxv)
Definition: RenderTypes.h:4490
real x(real v)
Definition: RenderTypes.h:638
virtual void setMaxNodes(sval num)
Definition: RenderTypes.h:4005
virtual Light * createLight()
Create a light object.
Definition: RenderTypes.h:4134
int sortTupleSecondCB(const void *v1, const void *v2)
Definition: RenderTypes.h:157
virtual const char * what() const
Definition: RenderTypes.h:1449
Definition: RenderTypes.h:262
real distTo(const vec3 v) const
Returns the distance from `v&#39; to the projection of `v&#39; on the ray, which is at getPosition(distTo(v))...
Definition: RenderTypes.h:3323
virtual void setNodes(Vec3Matrix *nodes)
Definition: RenderTypes.h:2267
void matop(const Matrix< R > &mat, sval minrow=0, sval mincol=0, sval maxrow=-1, sval maxcol=-1)
Apply the operation OpType::op to every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in the matri...
Definition: RenderTypes.h:1833
unsigned long long u64
Definition: RenderTypes.h:104
configmap map
Definition: RenderTypes.h:4057
Definition: RenderTypes.h:1452
void applyFunc(T(*op)(Ctx, const T &, sval, sval), Ctx ctx, sval minrow=0, sval mincol=0, sval maxrow=-1, sval maxcol=-1)
Apply the function `op&#39; to each cell from (minrow,mincol) to (maxrow-1,maxcol-1), passing in `ctx&#39; as...
Definition: RenderTypes.h:1809
void setBuff(float *v) const
Definition: RenderTypes.h:642
virtual void setSpecular(const color &c)
Sets the specular color, if usesInternalAlpha() returns true the alpha value will be set the internal...
Definition: RenderTypes.h:2973
RenderAdapter * getRenderAdapter(Config *config)
std::string getSharedDir()
Definition: RenderTypes.cpp:40
virtual bool usesDepthCheck() const
Definition: RenderTypes.h:2952
double delta
Definition: RenderTypes.h:310
#define unlock_mutex
Definition: RenderTypes.h:363
Definition: RenderTypes.h:443
virtual ~ControlCurve()
Definition: RenderTypes.h:2446
virtual void addNode(sval ribbon, const vec3 &pos, const color &col, real width, const rotator &rot=rotator(), real tex=0.0)
Definition: RenderTypes.h:4011
virtual void updateSpectrum()
Definition: RenderTypes.h:3008
virtual void setVisible(bool isVisible)
Set whether this light is currently illuminating or not.
Definition: RenderTypes.h:3046
virtual void clampTexAddress(bool use)
Definition: RenderTypes.h:2992
Definition: RenderTypes.h:488
void remove(indexval index)
Definition: RenderTypes.h:2408
virtual void fillRealMatrix(RealMatrix *mat)
Transfer the image data into the given matrix.
Definition: RenderTypes.h:3759
real _z
Definition: RenderTypes.h:996
Definition: RenderTypes.h:1586
Definition: RenderTypes.h:481
realtriple calculateTriPlaneSlice(const vec3 &planept, const vec3 &planenorm, const vec3 &a, const vec3 &b, const vec3 &c)
Definition: RenderTypes.cpp:516
T lerpXi(const T &val, const T &minv, const T &maxv)
Definition: RenderTypes.h:127
virtual void fillColor(const ColorMatrix *mat, indexval depth)
Definition: RenderTypes.h:2322
std::vector< indextriple > intersectsTriMesh(const Vec3Matrix *const nodes, const IndexMatrix *const inds, const Vec3Matrix *const centers, const RealMatrix *const radii2, sval numResults=0, sval excludeInd=-1) const
Definition: RenderTypes.h:3506
virtual std::vector< std::string > getFieldNames() const
Lists the names of field matrices stored.
Definition: RenderTypes.h:2285
virtual void setAABB(const vec3 &minv, const vec3 &maxv)
Definition: RenderTypes.h:3969
bool isShared() const
Returns true if the matrix is allocated in shared memory.
Definition: RenderTypes.h:1685
void fill(const RealMatrix *pos, const Matrix< T > *ctrls)
Definition: RenderTypes.h:2381
Definition: RenderTypes.h:1585
Ray(const vec3 &pos, const vec3 &dir)
Definition: RenderTypes.h:3286
real _z
Definition: RenderTypes.h:624
std::map< strpair, std::string > configmap
Definition: RenderTypes.h:4055
virtual bool isCameraOriented() const
Definition: RenderTypes.h:3999
mat4(const real *mat)
Definition: RenderTypes.h:887
T third
Definition: RenderTypes.h:267
bool isXFunc
Definition: RenderTypes.h:2612
vec3 getPlaneXi(const vec3 &pos, const vec3 &planepos, const rotator &orientinv, const vec3 &dimvec)
Definition: RenderTypes.h:4615
virtual void setAspectRatio(real rat)
Definition: RenderTypes.h:3820
void catmullRomSpline(real t, real *coeffs)
Produces the 4 coefficients for a Catmull-Rom spline in [value 1, value 2, derivative 1...
Definition: RenderTypes.cpp:409
void cubicInterpMatrices(real t, const Matrix< T > *v1, const Matrix< T > *v2, const Matrix< T > *m1, const Matrix< T > *m2, const Matrix< T > *result)
Definition: RenderTypes.h:4432
const char * getType() const
Definition: RenderTypes.h:1679
Matrix< T > * reshape(const char *name, sval n, sval m, bool isShared=false) const
Definition: RenderTypes.h:1795
virtual vec3 getScreenPosition(vec3 pos) const
Returns the (x,y) screen coordinate of the vector `pos&#39; as drawn with the current camera configuratio...
Definition: RenderTypes.h:3798
real dot(const vec3 &v) const
Return the dot product of `this&#39; and `v&#39;.
Definition: RenderTypes.h:663
Definition: RenderTypes.h:479
virtual rotator getRotation() const
Definition: RenderTypes.h:3792
i32 hash() const
Definition: RenderTypes.h:1359
virtual const char * getTexture() const
Definition: RenderTypes.h:2958
virtual bool usesTexFiltering() const
Definition: RenderTypes.h:2954
real m00
Definition: RenderTypes.h:878
Definition: RenderTypes.h:3056
virtual sval getWidth() const
Get the image width.
Definition: RenderTypes.h:3747
mat4 toMatrix() const
Definition: RenderTypes.h:1352
virtual void setBlendMode(BlendMode bm)
Definition: RenderTypes.h:2985
~Mutex()
Definition: RenderTypes.h:406
virtual bool isCameraAligned() const
Definition: RenderTypes.h:4041
Definition: RenderTypes.h:1411
#define lock_mutex
Definition: RenderTypes.h:362
virtual color getColor() const
Definition: RenderTypes.h:4034
T * createShared()
Create a shared segment and return a mapped pointer to it.
Definition: RenderTypes.h:2123
virtual Material * createMaterial(const char *name)
Create a material object of the given name.
Definition: RenderTypes.h:4128
T swapEndian32(T t)
Definition: RenderTypes.h:235
virtual void setRotation(const rotator &r)
Set the figure&#39;s rotation.
Definition: RenderTypes.h:3862
virtual sval numVertices() const
Returns number of total vertices.
Definition: RenderTypes.h:3224
virtual void setMaterial(const Material *mat)
Set&#39;s the figure&#39;s material.
Definition: RenderTypes.h:3885
real pos(indexval index) const
Definition: RenderTypes.h:2403
virtual void setUp(const vec3 &v)
Definition: RenderTypes.h:3811
virtual vec3 getTexXiPos(vec3 pos) const
Definition: RenderTypes.h:3972
virtual real getGPUParamReal(ProgramType pt, const std::string &name)
Definition: RenderTypes.h:2962
virtual void setTexAABB(const vec3 &minv, const vec3 &maxv)
Definition: RenderTypes.h:3967
virtual bool isCullBackfaces() const
Definition: RenderTypes.h:2956
bool isParentProc()
Definition: RenderTypes.h:220
T * data
Definition: RenderTypes.h:1613
Spectrum(const std::string &name="")
Definition: RenderTypes.h:2705
vec3(real x, real y, real z=0)
Construct a vector with the given components.
Definition: RenderTypes.h:630
virtual vec3 getNode(sval ribbon, sval node)
Definition: RenderTypes.h:4013
quadruple(const quadruple< F, S, T, U > &t)
Definition: RenderTypes.h:285
virtual void cullBackfaces(bool cull)
Definition: RenderTypes.h:2993
virtual void calculateDerivs()
Definition: RenderTypes.h:2647
Definition: RenderTypes.h:1591
rotator(real m00, real m01, real m02, real m10, real m11, real m12, real m20, real m21, real m22)
Defines a rotation from the significant 3x3 components of a 4x4 rotation matrix.
Definition: RenderTypes.h:1059
virtual void clearRibbons()
Definition: RenderTypes.h:4008
void setAlwaysHighQuality(bool val)
Set whether to force high quality rendering.
Definition: RenderTypes.h:4164
virtual void setVisible(bool isVisible)
Sets the figure&#39;s visibility.
Definition: RenderTypes.h:3902
Represents a ray emanating from a point and moving in a direction. It provides methods for doing inte...
Definition: RenderTypes.h:3277
static R op(const LH &lh, const RH &rh)
Definition: RenderTypes.h:1585
virtual sval getHeight() const
Definition: RenderTypes.h:3832
Light()
Definition: RenderTypes.h:3018
RenderException(const std::string msg, const char *file, int line)
Definition: RenderTypes.h:1439
virtual void setBillboardPos(indexval index, const vec3 &pos)
Definition: RenderTypes.h:3951
Definition: RenderTypes.h:2252
virtual ~RenderAdapter()
Definition: RenderTypes.h:4202
virtual bool usesVertexColor() const
Definition: RenderTypes.h:2949
virtual void removeRibbon(sval ribbon)
Definition: RenderTypes.h:4009
virtual void setNumRibbons(sval num)
Definition: RenderTypes.h:4002
sval _n
Definition: RenderTypes.h:1615
virtual void renderToFile(const std::string &filename, sval width, sval height, TextureFormat format=TF_RGB24, real stereoOffset=0.0)
Create an offscreen texture, render to it, then write the contents to the file `filename&#39;, assuming it&#39;s extension is for an understood format.
Definition: RenderTypes.h:3839
void normThis()
Normalizes `this&#39;, or do nothing if zero-length.
Definition: RenderTypes.h:683
quadruple< int, int, int, int > calculateBoundSquare(const Matrix< T > *const mat, const T &threshold)
Returns the bounding box (minx,miny,maxx,maxy) in matrix coordinates containing all values in `mat&#39; g...
Definition: RenderTypes.h:4451
void checkNotShared() const
Definition: RenderTypes.h:2036
virtual Ray * getProjectedRay(real x, real y, bool isAbsolute=true) const
Definition: RenderTypes.h:3788
real z() const
Definition: RenderTypes.h:636
rotator rot
Definition: RenderTypes.h:3574
virtual ~Texture()
Definition: RenderTypes.h:2311
virtual void clearSpectrum()
Definition: RenderTypes.h:2709
real distToSq(const vec3 &v) const
Return the squared distance from `this&#39; to `v&#39;; this is faster than dist().
Definition: RenderTypes.h:673
ColorMatrix * cols
Definition: RenderTypes.h:3163
float b(float val)
Definition: RenderTypes.h:567
void reorderColumns(const sval *orderinds)
Definition: RenderTypes.h:1891
virtual sval numIndices() const
Returns the number of index sets.
Definition: RenderTypes.h:3147
real m33
Definition: RenderTypes.h:878
Definition: RenderTypes.h:3015
std::pair< indexval, realtriple > indextriple
Definition: RenderTypes.h:291
color(const rgba &c)
Definition: RenderTypes.h:557
void ats(sval n, sval m, const T &t)
Definition: RenderTypes.h:1919
color interpolate(real val, const color &col) const
Linearly interpolate between `this&#39; and `col&#39;, val==0.0 yields `this&#39;, val==1.0 yields `col&#39;...
Definition: RenderTypes.h:583
static real swap(real t)
Definition: RenderTypes.h:1592
virtual vec3 getNormal(int i) const
Returns the i&#39;th normal, i<numVertices()
Definition: RenderTypes.h:3119
virtual vec3 getUVWCoord(int i) const
Returns the i&#39;th UVW texture coord, i<numVertices()
Definition: RenderTypes.h:3222
void sub(const R &r, sval minrow=0, sval mincol=0, sval maxrow=-1, sval maxcol=-1)
Subtract `r&#39; from every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in the matrix.
Definition: RenderTypes.h:1850
virtual vec3 getWorldPosition(real x, real y, bool isAbsolute=true) const
Returns the world position of screen coordinate (x,y), which is either absolute pixel coordinates or ...
Definition: RenderTypes.h:3801
std::string name
Definition: RenderTypes.h:2255
virtual void setNode(sval ribbon, sval node, const vec3 &pos, const color &col, real width, const rotator &rot=rotator(), real tex=0.0)
Definition: RenderTypes.h:4012
void checkNull(const std::string valuename, const void *val, const char *file=NULL, int line=-1)
Definition: RenderTypes.h:1474
virtual void setAttenuation(real range, real constant=0.0f, real linear=1.0f, real quad=0.0f)
Set the attenuation values for spot and point lights.
Definition: RenderTypes.h:3043
Definition: RenderTypes.h:2610
bool _isInverse
Definition: RenderTypes.h:3575
virtual sval indexWidth(int i) const
Returns the width of index set i, i<numIndices(). All index sets for now are assumed to be the same w...
Definition: RenderTypes.h:3272
def toMatrix(mtype)
Definition: VTKPlugin.py:76
virtual void setBillboardColor(indexval index, const color &col)
Definition: RenderTypes.h:3953
vec3 sign() const
Return a vector with each component of `this&#39; replaced with 1 if positive otherwise -1...
Definition: RenderTypes.h:659
IndexException(std::string name, size_t val, size_t maxval)
Definition: RenderTypes.h:1396
T bilerpMatrix(const Matrix< T > *mat, real x, real y)
Definition: RenderTypes.h:4569
void resize(sval reserveNum=0)
Definition: RenderTypes.h:2058
virtual bool hasUVWCoord() const
Returns true if the buffer contains texture coord data.
Definition: RenderTypes.h:3077
virtual Texture * createTexture(const char *name, sval width, sval height, sval depth, TextureFormat format)
Create a 3D texture with the given name, dimensions, and format. Textures are always 3D but a `depth&#39;...
Definition: RenderTypes.h:4140
bool isInUnitCube(real margin=0.0) const
Returns true if each component is on the interval [0,1]; this exact within a value of `margin&#39; in the...
Definition: RenderTypes.h:724
virtual void setTransparent(bool isTrans)
Set the transparency state of the figure, this doesn&#39;t actually change data but affects how the rende...
Definition: RenderTypes.h:3912
vecfunc uvwfunc
Definition: RenderTypes.h:3110
virtual const char * getFilename() const
Definition: RenderTypes.h:2314
static R op(const LH &lh, const RH &rh)
Definition: RenderTypes.h:1586
unsigned char u8
Definition: RenderTypes.h:102
virtual color getColor(int i) const
Returns the i&#39;th color, i<numVertices()
Definition: RenderTypes.h:3221
virtual bool isOverlay() const
Returns true if the figure is part of the UI overlay rather than an object in space.
Definition: RenderTypes.h:3909
bool inAABB(const vec3 &minv, const vec3 &maxv) const
Returns true if the vector is within the axis-aligned bounding box defined by the given min and max c...
Definition: RenderTypes.h:701
void toMatrix(real *mat) const
Fill in the 4x4 matrix representing this transform. This assumes `mat&#39; has length 16...
Definition: RenderTypes.h:3692
std::map< std::string, std::string > metamap
Definition: RenderTypes.h:1497
int i32
Definition: RenderTypes.h:100
TimingObject(const std::string &label, bool doPrint=true)
Definition: RenderTypes.h:315
bool isNan(real v)
Returns true if `v&#39; is NaN.
Definition: RenderTypes.h:179
void append(const Matrix< T > &t)
Append `t&#39; to the bottom of `this&#39;, throws exception if shared or if columns of `t&#39; and `this&#39; don&#39;t ...
Definition: RenderTypes.h:1960
transform(real x, real y, real z, real sx, real sy, real sz, real yaw, real pitch, real roll, bool isInv=false)
Define a transform with translation, scale, and Euler angle values.
Definition: RenderTypes.h:3586
virtual void setLinearAlpha(bool b)
Definition: RenderTypes.h:2808
void setMinVals(const vec3 &v)
Set each component of `this&#39; to minimum of its component and the equivalent in `v&#39;.
Definition: RenderTypes.h:678
virtual ~Figure()
Definition: RenderTypes.h:3853
Image objects represented loaded image files. These are used to access image data in code rather than...
Definition: RenderTypes.h:3739
void addShared(const std::string &name)
Definition: RenderTypes.cpp:45
virtual void addSpectrumValue(real pos, color value)
Add a color value to the spectrum at the given position then resort the spectrum. ...
Definition: RenderTypes.h:2730
real m20
Definition: RenderTypes.h:878
static indexval conv(const char *str)
Definition: RenderTypes.h:4233
bool onPlane(const vec3 &planept, const vec3 &planenorm) const
Returns true if the vector lies on the plane defined by the point `planept&#39; and normal `planenorm&#39;...
Definition: RenderTypes.h:721
virtual ~Camera()
Definition: RenderTypes.h:3776
bool runblock
Definition: RenderTypes.h:377
The all-important 3-space vector type. Note the lack of virtual members implies no vtable pointer...
Definition: RenderTypes.h:622
Mutex()
Definition: RenderTypes.h:394
real m02
Definition: RenderTypes.h:878
#define SAFE_DELETE(p)
Definition: RenderTypes.h:90
virtual vec3 getOrientation() const
Definition: RenderTypes.h:4000
virtual void setVAlign(VAlignType align)
Definition: RenderTypes.h:4025
virtual color getEmissive() const
Definition: RenderTypes.h:2940
An IndexBuffer is used by Figure objects to read in the topologies for the figures to render...
Definition: RenderTypes.h:3081
const char * meta(const char *key) const
Returns the value associated with the given key or the empty string if none is stored.
Definition: RenderTypes.h:1526
T clamp(const T &val, const T &minval, const T &maxval)
Definition: RenderTypes.h:117
float r() const
Definition: RenderTypes.h:560
Definition: RenderTypes.h:3994
static vec3 Y()
Returns the Y-axis unit vector.
Definition: RenderTypes.h:866
virtual const char * getName() const
Definition: RenderTypes.h:2313
Definition: RenderTypes.h:495
mat4(real m00, real m01, real m02, real m03, real m10, real m11, real m12, real m13, real m20, real m21, real m22, real m23, real m30, real m31, real m32, real m33)
Definition: RenderTypes.h:888
virtual void saveScreenshot(const char *filename, Camera *c=NULL, int width=0, int height=0, real stereoOffset=0.0, TextureFormat tf=TF_RGB24)
Save a screenshot to the given filename taken from the given camera, or of the whole 3D window if thi...
Definition: RenderTypes.h:4149
unsigned long _WId
Definition: RenderTypes.h:78
virtual sval getDepth() const
Get the image depth.
Definition: RenderTypes.h:3751
static int compZ(const void *v1, const void *v2)
Definition: RenderTypes.h:854
Definition: RenderTypes.h:452
virtual ~IndexBuffer()
Definition: RenderTypes.h:3084
Matrix(const char *name, const char *type, const T *array, sval n, sval m, bool isShared=false)
Constructor for converting a memory pointer into a Matrix, this will copy n*m values from `array&#39;...
Definition: RenderTypes.h:1652
triple(const F &first, const S &second, const T &third)
Definition: RenderTypes.h:270
realpair intersectsAABB(const vec3 &minv, const vec3 &maxv) const
Definition: RenderTypes.h:3346
virtual sval getWidth() const
Definition: RenderTypes.h:2315
void clear()
Definition: RenderTypes.h:1727
virtual void removeAlphaCtrl(indexval index)
Definition: RenderTypes.h:2790
real getImageStackValue(const std::vector< RealMatrix *> &stack, const vec3 &pos)
Definition: RenderTypes.cpp:640
ProgramType
Definition: RenderTypes.h:485
virtual sval getNumPlanes() const
Definition: RenderTypes.h:3962
real m30
Definition: RenderTypes.h:878
virtual void setRotation(const rotator &r)
Definition: RenderTypes.h:3814
vec3 dir
Definition: RenderTypes.h:3279
void meta(const char *key, const char *val)
Add the given key-value pair to the metadata, or overwrite an existing if present. Does nothing if either argument is NULL.
Definition: RenderTypes.h:1532
virtual bool isPointInViewport(int x, int y) const
Definition: RenderTypes.h:3834
virtual const char * getName() const
Definition: RenderTypes.h:3778
virtual real getHeight() const
Definition: RenderTypes.h:3943
std::string name
Definition: RenderTypes.h:2702
Matrix(const char *name, const char *type, const char *sharedname, const char *serialmeta, sval n, sval m)
Constructor for unpickling only, do not use.
Definition: RenderTypes.h:1642
real triArea(const vec3 &b, const vec3 &c) const
Returns the area of a triangle defined by `this&#39;, `b&#39;, and `c&#39;.
Definition: RenderTypes.h:800
#define PLATFORM_ID
Definition: RenderTypes.h:77
Ctx context
Definition: RenderTypes.h:3112
IndexMatrix * indices
Definition: RenderTypes.h:3233
static int compX(const void *v1, const void *v2)
Definition: RenderTypes.h:844
~TimingObject()
Definition: RenderTypes.h:328
bool hasValue(const char *group, const char *name) const
Definition: RenderTypes.h:4064
real * getPointer() const
Definition: RenderTypes.h:894
static T conv(const char *str)
Definition: RenderTypes.h:4231
virtual void setAmbientLight(const color &c)
Set the scene ambient light to the given color value.
Definition: RenderTypes.h:4119
real calculateLinePlaneSlice(const vec3 &planept, const vec3 &planenorm, const vec3 &a, const vec3 &b)
Definition: RenderTypes.cpp:546
virtual real getPointSizeMax() const
Definition: RenderTypes.h:2944
bool isLinearFunc() const
Definition: RenderTypes.h:2618
std::pair< vec3, vec3 > calculateBoundBox(const Vec3Matrix *mat)
Definition: RenderTypes.cpp:280
T third
Definition: RenderTypes.h:280
void convertByteStreamToRealMatrix(const char *stream, RealMatrix *mat)
Definition: RenderTypes.cpp:250
virtual color getGPUParamColor(ProgramType pt, const std::string &name)
Definition: RenderTypes.h:2964
virtual sval numVertices() const
Returns number of total vertices.
Definition: RenderTypes.h:3123
virtual sval numVertices() const
Returns number of total vertices.
Definition: RenderTypes.h:3071
Definition: RenderTypes.h:462
virtual vec3 getPosition(bool isDerived=false) const
Get the figure&#39;s position in world space.
Definition: RenderTypes.h:3874
MatrixVertexBuffer(Vec3Matrix *vecs, ColorMatrix *cols=NULL, IndexMatrix *extinds=NULL)
Create the buffer from these matrices, vecs.m() in (1,2,4). The caller is responsible for deleting th...
Definition: RenderTypes.h:3170
virtual sval getIndex(int i, int j) const
Returns the w&#39;th value of index set i.
Definition: RenderTypes.h:3273
sval numvertices
Definition: RenderTypes.h:3111
real lenSq() const
Return the squared length of `this&#39;; this is faster than len().
Definition: RenderTypes.h:667
void setMaxVals(const vec3 &v)
Set each component of `this&#39; to maximum of its component and the equivalent in `v&#39;.
Definition: RenderTypes.h:680
virtual void removeSpectrumValue(indexval index)
Remove the spectrum value at the given index.
Definition: RenderTypes.h:2768
virtual bool isLinearAlpha() const
Definition: RenderTypes.h:2814
virtual void setPosition(vec3 &v)
Set the position for this light, only meaningful for point and spot lights.
Definition: RenderTypes.h:3022
virtual void setAlphaCtrl(vec3 v, indexval index)
Definition: RenderTypes.h:2796
void addRows(sval num)
Add `num&#39; rows, throws exception if shared.
Definition: RenderTypes.h:1954
sval numindices
Definition: RenderTypes.h:3140
virtual Image * loadImageFile(const std::string &filename)
Load an image from the given filename.
Definition: RenderTypes.h:4137
virtual bool isTransparent() const
Returns true if the figure includes transparent elements.
Definition: RenderTypes.h:3907
triple< real, real, real > realtriple
Definition: RenderTypes.h:290
virtual vec3 getAlphaCtrl(indexval index) const
Definition: RenderTypes.h:2779
real len() const
Return the length of `this&#39;.
Definition: RenderTypes.h:665
virtual T at(real tt) const
Definition: RenderTypes.h:2536
void setScale(const vec3 &v)
Definition: RenderTypes.h:3597
Matrix< T > * clone(const char *newname=NULL, bool isShared=false) const
Copy the contents of this matrix into a newly allocated one (which can be shared if isShared is true)...
Definition: RenderTypes.h:1669
virtual vec3 getGPUParamVec3(ProgramType pt, const std::string &name)
Definition: RenderTypes.h:2963
virtual ~MatrixVertexBuffer()
Definition: RenderTypes.h:3208
vec3 getPosition(real t=0) const
Get a position on the line at distance t, ie. t=0 is the origin.
Definition: RenderTypes.h:3297
rotator norm() const
Definition: RenderTypes.h:1285
Definition: RenderTypes.h:373
virtual sval numNodes(sval ribbon) const
Definition: RenderTypes.h:4004
static color swap(color t)
Definition: RenderTypes.h:1595
real m[4][4]
Definition: RenderTypes.h:883
virtual void copySpectrumFrom(const Spectrum *s)
Definition: RenderTypes.h:2716
virtual void setCameraVisibility(const Camera *cam, bool isVisible)
Definition: RenderTypes.h:3928
virtual ~Material()
Definition: RenderTypes.h:2905
virtual HAlignType getHAlign() const
Definition: RenderTypes.h:4037
void fill(const T &t)
Set every cell of the matrix to the given value.
Definition: RenderTypes.h:1754
virtual void useSpectrumTexture(bool use)
Definition: RenderTypes.h:2998
vec3 norm() const
Return the normalized version of `this&#39;, or a zero vector if `this&#39; is zero-length.
Definition: RenderTypes.h:669
PositionQueue< color > spec
Definition: RenderTypes.h:2698
void setPosition(const vec3 &v)
Set the origin of the ray, this is what getPosition(0) shall return.
Definition: RenderTypes.h:3303
static void parse(const char *line, sval numvals, vec3 *list)
Definition: RenderTypes.h:4257
virtual void setSpaceWidth(real width)
Definition: RenderTypes.h:4028
virtual sval numPoints() const
Definition: RenderTypes.h:2464
RenderScene()
Definition: RenderTypes.h:4107
void setM(sval _newm)
Reshape to fit this many columns, does not allocate new columns but re-arranges existing ones and tru...
Definition: RenderTypes.h:1941
virtual vec3 getScale(bool isDerived=false) const
Get the figure&#39;s scale values.
Definition: RenderTypes.h:3876
virtual bool setParameter(const std::string &param, const std::string &val)
Definition: RenderTypes.h:2349
Vec3Curve alphacurve
Definition: RenderTypes.h:2700
void setDirection(const vec3 &v)
Set the direction the ray is pointing.
Definition: RenderTypes.h:3306
virtual const char * what() const
Definition: RenderTypes.h:1408
virtual void setNearClip(real dist)
Definition: RenderTypes.h:3816
virtual sval numSpectrumValues() const
Get the number of spectrum values.
Definition: RenderTypes.h:2762
Material()
Definition: RenderTypes.h:2904
virtual void setAmbient(const color &c)
Definition: RenderTypes.h:2969
Ctx context
Definition: RenderTypes.h:3141
bool getAlwaysHighQuality() const
Returns whether to always render in high quality mode.
Definition: RenderTypes.h:4170
virtual bool setGPUParamInt(ProgramType pt, const std::string &name, int val)
Definition: RenderTypes.h:3003
void div(const R &r, sval minrow=0, sval mincol=0, sval maxrow=-1, sval maxcol=-1)
Divide every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in the matrix by `r&#39;.
Definition: RenderTypes.h:1862
bool inOBB(const vec3 &center, const vec3 &hx, const vec3 &hy, const vec3 &hz) const
Returns true if the vector is within the oriented bounding box defined by the given center position a...
Definition: RenderTypes.h:709
Definition: RenderTypes.h:463
std::string label
Definition: RenderTypes.h:313
real frand()
Definition: RenderTypes.h:185
virtual ~VertexBuffer()
Definition: RenderTypes.h:3059
Definition: main.py:1
virtual void setMaterial(const char *mat)
Set the figure&#39;s material, this must name an existing material.
Definition: RenderTypes.h:3883
virtual size_t getDataSize() const
Get the image data size in bytes.
Definition: RenderTypes.h:3753
sval n() const
Get the number of rows.
Definition: RenderTypes.h:1745
Matrix< T > ctrls
Definition: RenderTypes.h:2441
real dot(const rotator &r) const
Definition: RenderTypes.h:1280
strpair getPair(const char *group, const char *name) const
Definition: RenderTypes.h:4087
bool _isShared
Definition: RenderTypes.h:1617
real m32
Definition: RenderTypes.h:878
real intersectsPlane(const vec3 &planepos, const vec3 &planenorm) const
Definition: RenderTypes.h:3336
virtual void fillData(const VertexBuffer *vb, const IndexBuffer *ib, bool deferFill=false, bool doubleSided=false)
Definition: RenderTypes.h:3949
virtual ~MemException()
Definition: RenderTypes.h:1427
virtual void setTransform(const transform &t)
Set position, rotation, and scale for this figure simultaneously.
Definition: RenderTypes.h:3864
virtual void removeCtrlPoint(indexval index)
Definition: RenderTypes.h:2463
virtual void setType(ProgramType pt)
Definition: RenderTypes.h:2333
void setName(const char *name)
Definition: RenderTypes.h:1681
vec3 trans
Definition: RenderTypes.h:3572
real x() const
Definition: RenderTypes.h:1147
U fourth
Definition: RenderTypes.h:281
virtual void useLighting(bool use)
Definition: RenderTypes.h:2987
virtual int getGPUParamInt(ProgramType pt, const std::string &name)
Definition: RenderTypes.h:2961
real y(real v)
Definition: RenderTypes.h:639
virtual Image * renderToImage(sval width, sval height, TextureFormat format=TF_RGB24, real stereoOffset=0.0)
Create an offscreen texture, render to it, then blit the contents to the returned Image object...
Definition: RenderTypes.h:3843
void closeShared(T *ptr)
Unmap the given shared segment, and delete it if this object created it.
Definition: RenderTypes.h:2202
virtual sval getDepth() const
Definition: RenderTypes.h:2317
virtual std::string getSourceCode() const
Return the text of the source for this program.
Definition: RenderTypes.h:2347
Matrix< indexval > IndexMatrix
Definition: RenderTypes.h:2240
virtual void setOrientation(const vec3 &orient)
Definition: RenderTypes.h:3998
void mulm(const Matrix< R > &mat, sval minrow=0, sval mincol=0, sval maxrow=-1, sval maxcol=-1)
Multiply every cell from (minrow,mincol) to (maxrow-1,maxcol-1) in the matrix by the same in `mat&#39;...
Definition: RenderTypes.h:1880
virtual ~Image()
Definition: RenderTypes.h:3742
int sortTupleFirstCB(const void *v1, const void *v2)
Definition: RenderTypes.h:152
static vec3 posInfinity()
Definition: RenderTypes.h:859
virtual real getShininess() const
Definition: RenderTypes.h:2942
std::string serializeMeta() const
Turn all metadata name-value pairs into one string suitable for pickling.
Definition: RenderTypes.h:1554
virtual void setGlyphName(const std::string &name)
Definition: RenderTypes.h:3989
RenderException(const RenderException &op)
Definition: RenderTypes.h:1445
virtual const char * getMaterial() const
Get the figure&#39;s material name.
Definition: RenderTypes.h:3888
void convertUByteStreamToRealMatrix(const char *stream, RealMatrix *mat)
Definition: RenderTypes.cpp:240
virtual void setTransform(const vec3 &trans, const vec3 &scale, const rotator &rot)
Set position, rotation, and scale for this figure simultaneously.
Definition: RenderTypes.h:3866
void swapEndian()
Definition: RenderTypes.h:1909
bool pointInHex(vec3 pt, vec3 n1, vec3 n2, vec3 n3, vec3 n4, vec3 n5, vec3 n6, vec3 n7, vec3 n8)
Returns true if `pt&#39; is in the hex defined by (n1-n8)
Definition: RenderTypes.cpp:441
#define dPI
Definition: RenderTypes.h:81
virtual void setSpotlight(real radsInner, real radsOuter, real falloff=1.0f)
Make this a spot light with the given beam angles and falloff values.
Definition: RenderTypes.h:3040
Camera()
Definition: RenderTypes.h:3775
void setAt(const T &t, sval n, sval m=0)
Set the value at (n,m) to t.
Definition: RenderTypes.h:1930
Definition: RenderTypes.h:2695
Matrix< T > * subMatrix(const char *name, sval n, sval m=1, sval noff=0, sval moff=0, bool isShared=false) const
Create a submatrix from this one of dimensions (n,m), starting at row `noff&#39; and column `moff&#39;...
Definition: RenderTypes.h:1770
bool entered
Definition: RenderTypes.h:312
HAlignType
Definition: RenderTypes.h:492
std::string name
Definition: RenderTypes.h:1384
virtual bool isSecondaryCamera()
Definition: RenderTypes.h:3836
virtual color getColor(int i) const
Returns the i&#39;th color, i<numVertices()
Definition: RenderTypes.h:3120
real y() const
Definition: RenderTypes.h:1148
MatrixIndexBuffer(const IndexBuffer *buf)
Copy the data from `buf&#39; into internal matrices which this object is responsible for and will delete ...
Definition: RenderTypes.h:3243
sval getIndex(sval n, sval m) const
Get the index corresponding to cell (n,m)
Definition: RenderTypes.h:2022
BlendMode
Definition: RenderTypes.h:459
static int compY(const void *v1, const void *v2)
Definition: RenderTypes.h:849
virtual void setGlyphScale(vec3 v)
Definition: RenderTypes.h:3987
std::string _sharedname
Definition: RenderTypes.h:1611
virtual VAlignType getVAlign() const
Definition: RenderTypes.h:4036
virtual void setShininess(real c)
Set the amount of specular hightlighting to apply.
Definition: RenderTypes.h:2976
transform directional() const
Get the directional version of this transform which is suitable for transforming directional vectors ...
Definition: RenderTypes.h:3686
MutexType _mutex
Definition: RenderTypes.h:370
vecfunc vertfunc
Definition: RenderTypes.h:3107
real getPitch() const
Get the pitch angle (x-axis rotation in radians)
Definition: RenderTypes.h:1152
Definition: RenderTypes.h:1587
Definition: RenderTypes.h:482
virtual sval getPlaneIntersects(vec3 planept, vec3 planenorm, vec3 buffer[6][2], bool transformPlane=false, bool isXiPoint=false)
Definition: RenderTypes.h:3976
Definition: RenderTypes.h:450
virtual void paint()
Definition: RenderTypes.h:4205
void convertStreamToRealMatrix(const T *stream, RealMatrix *mat)
Fill a given RealMatrix with data from the given byte stream which contains values of various types...
Definition: RenderTypes.h:4317
vec3 abs() const
Return a vector with the absolute value of each component of `this&#39;.
Definition: RenderTypes.h:655
virtual real getFarClip() const
Definition: RenderTypes.h:3829
Definition: RenderTypes.h:2359
T & at(sval n, sval m=0) const
Same as getAt except returning a reference and no bounds check.
Definition: RenderTypes.h:1917
virtual void setBGObject(color col, bool enabled)
Set the background skybox to the given color if `enabled&#39; is true, otherwise disable it...
Definition: RenderTypes.h:4158
bool isParallel(const vec3 &other) const
Returns true if `other&#39; is parallel with this vector, ie. they represent the same or opposite directi...
Definition: RenderTypes.h:727
virtual ProgramType getType() const
Definition: RenderTypes.h:2334
virtual ~TextFigure()
Definition: RenderTypes.h:4020
void convertShortStreamToRealMatrix(const char *stream, RealMatrix *mat)
Definition: RenderTypes.cpp:255
void readTextFileMatrix(const std::string &filename, sval numHeaders, Matrix< T > *mat)
Reads the text file into the given matrix, ignoring the header of integers and using the dimensions o...
Definition: RenderTypes.h:4280
virtual bool setGPUParamReal(ProgramType pt, const std::string &name, real val)
Definition: RenderTypes.h:3004
virtual void setSecondaryCamera(bool selective)
Definition: RenderTypes.h:3824
virtual bool hasColor() const
Returns true if the buffer contains color data.
Definition: RenderTypes.h:3075
virtual bool usesPointAttenuation() const
Definition: RenderTypes.h:2946
virtual int numBillboards() const
Definition: RenderTypes.h:3947
virtual vec3 getLookAt() const
Definition: RenderTypes.h:3791
virtual void setTextHeight(real height)
Definition: RenderTypes.h:4027
void release()
Releases the mutex lock.
Definition: RenderTypes.h:433
realtriple intersectsTri(const vec3 &v0, const vec3 &v1, const vec3 &v2) const
Definition: RenderTypes.h:3463
T getAt(sval n, sval m=0) const
Get the element at (n,m) in the matrix, throws exception if `n&#39; or `m&#39; out of range.
Definition: RenderTypes.h:1927
void add(real pos, T val)
Definition: RenderTypes.h:2375
virtual TextureFormat getFormat() const
Get the loaded data&#39;s format.
Definition: RenderTypes.h:3745
virtual bool hasDataField(const char *name) const
Returns true if a field matrix of the given name is stored.
Definition: RenderTypes.h:2294
F first
Definition: RenderTypes.h:278
float _r
Definition: RenderTypes.h:531
IndexMatrix * extinds
Definition: RenderTypes.h:3164
vec3 getScale() const
Definition: RenderTypes.h:3591
indexpair indexOf(const T &t, sval aftern=0, sval afterm=0) const
Find the row-column pair in the matrix where `t&#39; is found, or indexpair(n(),0) if not found (None in ...
Definition: RenderTypes.h:2008
ValueException(const std::string valuename, const std::string msg, const char *file=NULL, int line=-1)
Definition: RenderTypes.h:1457
double real
Definition: RenderTypes.h:108
Matrix< color > ColorMatrix
Definition: RenderTypes.h:2241
real z() const
Definition: RenderTypes.h:1149
clock_t stop
Definition: RenderTypes.h:309
virtual std::string getFont() const
Definition: RenderTypes.h:4033
vec3 clamp(const vec3 &v1, const vec3 &v2) const
Return a vector whose components are clamped within the AABB defined by the given vectors...
Definition: RenderTypes.h:675
virtual void setParent(Figure *fig)
Set the parent of this figure, if `fig&#39; is transformed then the transformation is applied to this fig...
Definition: RenderTypes.h:3931
virtual RealMatrix * getDataField(const char *name) const
Get the field matrix of the given name, or NULL if not found.
Definition: RenderTypes.h:2291
virtual void resize(int x, int y, int width, int height)
Definition: RenderTypes.h:4206
vec3 planeNorm(const vec3 &v2, const vec3 &v3, const vec3 &farv) const
Returns the normal of a plane defined by `this&#39;, `v2&#39;, and `v3&#39; with `farv&#39; defined as below the plan...
Definition: RenderTypes.h:776
virtual sval indexWidth(int i) const
Returns the width of index set i, i<numIndices(). All index sets for now are assumed to be the same w...
Definition: RenderTypes.h:3148
virtual sval indexWidth(int i) const
Returns the width of index set i, i<numIndices(). All index sets for now are assumed to be the same w...
Definition: RenderTypes.h:3089
real basis_n_NURBS(sval ctrlpt, sval degree, real xi, const RealMatrix *knots)
Definition: RenderTypes.cpp:325
virtual quadruple< color, real, rotator, real > getNodeProps(sval ribbon, sval node)
Definition: RenderTypes.h:4014
virtual Material * clone(const char *name) const
Make a copy of this material with the given name.
Definition: RenderTypes.h:2908
long long i64
Definition: RenderTypes.h:101
void set(const rotator &r)
Copy the values of `r&#39; into `this&#39;.
Definition: RenderTypes.h:1126
Definition: RenderTypes.h:4051
#define MutexType
Definition: RenderTypes.h:360
virtual transform getTransform(bool isDerived=false) const
Get the figure&#39;s position, scale, and rotation transform.
Definition: RenderTypes.h:3880
virtual std::vector< std::string > getParameterNames() const
Definition: RenderTypes.h:2353
def group(iterable, width=2)
Definition: Utils.py:2148
triple< sval, sval, real > intersect
Definition: RenderTypes.h:292
RenderException(const std::string msg)
Definition: RenderTypes.h:1437
virtual std::pair< vec3, vec3 > getAABB() const
Definition: RenderTypes.h:3890
virtual vec3 getNormal(int i) const
Returns the i&#39;th normal, i<numVertices()
Definition: RenderTypes.h:3220
real m11
Definition: RenderTypes.h:878
void sort()
Definition: RenderTypes.h:2425
virtual color getColor(int i) const
Returns the i&#39;th color, i<numVertices()
Definition: RenderTypes.h:3066
void basis_NURBS_default(real u, real v, real w, sval ul, sval vl, sval wl, sval udegree, sval vdegree, sval wdegree, real *coeffs)
Definition: RenderTypes.cpp:370
std::vector< std::string > getMetaKeys() const
Definition: RenderTypes.h:1507
virtual bool hasNormal() const
Returns true if the buffer contains normal data.
Definition: RenderTypes.h:3073
std::pair< indexval, indexval > indexpair
Definition: RenderTypes.h:288
virtual void setProfiles(const std::string profiles)
Definition: RenderTypes.h:2355
std::string getPIDStr()
Definition: RenderTypes.h:195
vec3 scale
Definition: RenderTypes.h:3573
T lerp(const V &val, const T &v1, const T &v2)
Definition: RenderTypes.h:132
virtual void useDepthWrite(bool use)
Definition: RenderTypes.h:2990
Matrix< T > derivs
Definition: RenderTypes.h:2442
virtual void renderToStream(void *stream, sval width, sval height, TextureFormat format=TF_RGB24, real stereoOffset=0.0)
Create an offscreen texture, render to it, then blit the contents to `stream&#39;, which must be large en...
Definition: RenderTypes.h:3841
void copyFrom(const ControlCurve< T > *con)
Definition: RenderTypes.h:2448
float calculateTetVolume(vec3 a, vec3 b, vec3 c, vec3 d)
Returns the volume of the linear tetrahedron defined by (a,b,c,d), the volume will be negative for an...
Definition: RenderTypes.h:4396
ValueException(const ValueException &op)
Definition: RenderTypes.h:1467
virtual void addCtrlPoint(const vec3 &t)
Definition: RenderTypes.h:2620
real mat4Det(real a, real b, real c, real d, real e, real f, real g, real h, real i, real j, real k, real l, real m, real n, real o, real p)
Definition: RenderTypes.cpp:423
virtual mat4 getViewMatrix() const
Definition: RenderTypes.h:3794
Definition: RenderTypes.h:502
void setN(sval _newn)
Resize the matrix to have _newn rows, throws exception if shared.
Definition: RenderTypes.h:1933
virtual void setPosition(const vec3 &v)
Definition: RenderTypes.h:3809
virtual const char * what() const
Definition: RenderTypes.h:1471
void convertUShortStreamToRealMatrix(const char *stream, RealMatrix *mat)
Definition: RenderTypes.cpp:245