mqsdx  310.0.0
MQPluginSDK Extention Library / mqsdkをC++またはCLI(.Net)拡張するサポートライブラリ
 全て クラス 関数 変数 型定義 プロパティ グループ ページ
K:/Dancer_iMac/mqsdkUVTexIntegra/mqsdx/MQ0x.hpp
1 /*
2 First author tiritomato 2013.
3 
4 mqsdx is distributed under the GNU Lesser General Public License 3.0(LGPLv3).
5 
6 support blog (Japanese only)
7 http://d.hatena.ne.jp/tiri_tomato/
8 */
9 
10 #ifndef __MQSDKPlugIn0x_11_h__
11 #define __MQSDKPlugIn0x_11_h__
12 
13 #include <string.h>
14 #include <algorithm>
15 #include <functional>
16 #include <iomanip>
17 #include <regex>
18 #include <sstream>
19 #include <vector>
20 #define NOMINMAX
21 #define WIN32_LEAN_AND_MEAN
22 #include <windows.h>
23 #include <MQSetting.h>
24 #include <MQBasePlugin.h>
25 #include <MQPlugin.h>
26 
27 #include "impl/MQ0x_11.hpp"
28 
29 // Plugin Version Checker
30 #ifdef MQPLUGIN_VERSION
31  #if MQPLUGIN_VERSION < 0x0300
32  "MQx" need newer than mqsdk300
33  #endif
34 #else
35  "MQx" need newer than mqsdk300
36 #endif
37 
38 namespace MQ0x {
39 
69  template <typename T_OUT, const size_t default_seed = 17> struct CollectionHashCode {
70  public:
72  static const T_OUT defaultSeed = (T_OUT)default_seed;
74  CollectionHashCode( const T_OUT seed = defaultSeed ) : m_val( seed ) {}
80  template <typename T_IN> CollectionHashCode( const T_IN *pSrc, const size_t ct, const T_OUT seed = defaultSeed ) : m_val(seed) {
81  if ( pSrc != NULL ) for ( size_t i = 0; i < ct; i++ ) m_val = (m_val * (T_OUT)(sizeof(T_IN) * 8 - 1)) ^ pSrc[i];
82  }
87  CollectionHashCode( const char *pSrc, const T_OUT seed = defaultSeed ) : m_val(FromTrailCodeCollection(pSrc,'\0',seed)) {}
92  CollectionHashCode( const wchar_t *pSrc, const T_OUT seed = defaultSeed ) : m_val(FromTrailCodeCollection(pSrc,L'\0',seed)) {}
98  template <typename T_IN> static CollectionHashCode FromTrailCodeCollection( const T_IN *pSrc, const T_IN trailCode, const T_OUT seed = defaultSeed ) {
99  CollectionHashCode ret(seed);
100  if ( pSrc != NULL ) while ( (*pSrc) != trailCode ) ret.m_val = (ret.m_val * (T_OUT)(sizeof(T_IN) * 8 - 1)) ^ (*(pSrc++));
101  return ret;
102  }
104  operator T_OUT() { return m_val; }
105  private:
106  T_OUT m_val;
107  };
108 
115  template <typename T> struct HSV {
116  typedef T value_type;
117  T h;
118  T s;
119  T v;
120  HSV() : h(T(0)), s(T(0)), v(T(0)) {}
121  HSV(T h_, T s_, T v_) : h(h_), s(s_), v(v_) {}
122  HSV( const HSV& src ) : h(src.h), s(src.s), v(src.v) {}
123 
124  HSV( const MQColor& rgb ) : h(T(0)), s(T(0)), v(T(0)) {
125  const T min = T(std::min(std::min(rgb.r,rgb.g),rgb.b));
126  const T max = T(std::max(std::max(rgb.r,rgb.g),rgb.b));
127  const T delta = max - min;
128  v = max;
129  if(delta!=T(0)) {
130  s = delta / max;
131  if ( rgb.r == max ) h = (rgb.g-rgb.b) / delta;
132  else if (rgb.g == max) h = T(2) + (rgb.b-rgb.r) / delta;
133  else h = T(4) + (rgb.r-rgb.g) / delta;
134  h /= T(6);
135  if ( h < T(0) ) h += T(1);
136  }
137  }
143  bool isGrayScale( T threshold ) const { return threshold < s; }
146  bool isGrayScale() const { return isGrayScale(std::numeric_limits<T>::epsilon()); }
151  void normalize() {
152  h -= std::floor(h);
153  s = clamp01(s);
154  v = clamp01(v);
155  }
160  MQColor toMQColor() const {
161  const HSV normal = toNormalized(); // to [0,1]
162  if ( normal.isGrayScale() ) return MQColor( normal.v );
163  const T H = normal.h * T(6);
164  const int Hi = static_cast<int>(H);
165  const T fr = H - T(Hi);
166  const T m = normal.v * (T(1)-normal.s);
167  const T n = normal.v * (T(1)-normal.s*fr);
168  const T p = normal.v * (T(1)-normal.s*(T(1)-fr));
169  switch(Hi) {
170  case 0: return MQColor( float(normal.v), float(p), float(m) );
171  case 1: return MQColor( float(n), float(normal.v), float(m) );
172  case 2: return MQColor( float(m), float(normal.v), float(p) );
173  case 3: return MQColor( float(m), float(n), float(normal.v) );
174  case 4: return MQColor( float(p), float(m), float(normal.v) );
175  default: return MQColor( float(normal.v), float(m), float(n) );
176  }
177  }
182  HSV toNormalized() const { return HSV( h - std::floor(h), clamp01(s), clamp01(v) ); }
183  };
184 
198  struct SettingProxy {
199  public:
201  struct Handle {
202  friend struct SettingProxy;
203  template <typename T_MQPLUGIN> friend class PluginBase;
204  static Handle Empty() { return Handle(NULL); }
205  public:
206  Handle( MQBasePlugin* plugin ) { this->plugin = plugin; }
207  MQBasePlugin* plugin;
208  };
209  SettingProxy( Handle handle );
210  ~SettingProxy() { Close(); }
211  void Close();
212  bool Load(const char *name, bool& value, bool default_value=false);
213  bool Load(const char *name, int& value, int default_value=0);
214  bool Load(const char *name, unsigned int& value, unsigned int default_value=0);
215  bool Load(const char *name, float& value, float default_value=0.0f);
216  bool Load(const char *name, std::string& value, std::string default_value="");
217  bool Save(const char *name, const bool& value);
218  bool Save(const char *name, const int& value);
219  bool Save(const char *name, const unsigned int& value);
220  bool Save(const char *name, const float& value);
221  bool Save(const char *name, const char* value);
222  bool Save(const char *name, const std::string& value);
223  private:
224  SettingProxy(const SettingProxy&); // kill copy
225  SettingProxy& operator=(const SettingProxy&); // kill copy
226  MQBasePlugin* m_plugin;
227  MQSetting* m_setting;
228  };
229 
265  template <typename T_MQPLUGIN> class PluginBase : public T_MQPLUGIN {
266  public:
267  PluginBase( const char* productName, const char* pluginFullName, const char* pluginString ) :
268  m_productName( productName ),
269  m_pluginFullName( pluginFullName ),
270  m_pluginString( pluginString ),
271  m_idProduct( CollectionHashCode<DWORD>(productName) ),
272  m_idPlugin( CollectionHashCode<DWORD>(pluginFullName) )
273  {
274  }
275 
276  PluginBase( const char* productName, const char* pluginFullName, const char* pluginString,
277  const DWORD idProduct, const DWORD idPlugin ) :
278  m_productName( productName ),
279  m_pluginFullName( pluginFullName ),
280  m_pluginString( pluginString ),
281  m_idProduct( idProduct ),
282  m_idPlugin( idPlugin )
283  {
284  }
285 
287  virtual void GetPlugInID(DWORD *Product, DWORD *ID) {
288  *Product = m_idProduct;
289  *ID = m_idPlugin;
290  }
292  virtual const char *GetPlugInName() { return m_pluginFullName.c_str(); }
294  virtual const char *EnumString() { return m_pluginString.c_str(); }
296  const char* GetProductName() { return m_productName.c_str(); }
298  MQBasePlugin* BasePlugin() { return this; }
302  DWORD ProductID() { return m_idProduct; }
304  DWORD PluginID() { return m_idPlugin; }
305  protected:
306  const std::string m_productName;
307  const std::string m_pluginFullName;
308  const std::string m_pluginString;
309  const DWORD m_idProduct;
310  const DWORD m_idPlugin;
311  };
312 
313  namespace Math {
329  template <typename T> inline T moveTo( T from, T to, T mov ) {
330  mov = std::abs(mov);
331  if ( std::abs(from - to) < mov ) return to;
332  return from += ( from < to ? mov : -mov );
333  }
338  template <typename T> inline T clamp( T src, T min, T max ) {
339  return std::min( std::max(min,max), std::max( std::min(min,max), src) );
340  }
345  template <typename T> inline T clamp01( T src ) { return std::min(T(1),std::max(T(0),src)); }
350  template <typename T> inline T trunc( T src ) { if ( T(0) < src ) return std::floor(src); return std::ceil(src); }
353  }
354  using namespace Math;
355 
359 
360 #ifdef _DEBUG
361  static const std::vector<char>::size_type GetNameInitialBufferSize = 2;
362 #else
363  static const std::vector<char>::size_type GetNameInitialBufferSize = 32;
364 #endif
365 
368  inline bool DeleteMaterial( const MQDocument doc, const MQMaterial mat ) {
369  int index = prv_impl::GetIdentifiedIndex(doc, mat);
370  if ( index < 0 ) return false;
371  doc->DeleteMaterial(index); return true;
372  }
375  inline bool DeleteObject( const MQDocument doc, const MQObject obj ) {
376  int index = prv_impl::GetIdentifiedIndex(doc, obj);
377  if ( index < 0 ) return false;
378  doc->DeleteObject(index); return true;
379  }
392  inline std::string GetCountUpCloneableUniqueName( const MQDocument doc, const MQMaterial src, std::vector<char>* const buf = NULL ) {
393  return prv_impl::GetCountUpCloneableUniqueName(doc,src,buf);
394  }
407  inline std::string GetCountUpCloneableUniqueName( const MQDocument doc, const MQObject src, std::vector<char>* const buf = NULL ) {
408  return prv_impl::GetCountUpCloneableUniqueName(doc,src,buf);
409  }
412  inline MQMaterial GetMaterial( const MQDocument doc, const char* name ) { return prv_impl::GetNamed<MQMaterial>( doc, name ); }
417  inline MQMaterial GetMaterial( const MQDocument doc, const UINT id ) {
418  #if 0x0310 <= MQPLUGIN_VERSION
419  if ( doc != NULL ) return doc->GetMaterialFromUniqueID(id);
420  return NULL;
421  #else
422  return prv_impl::GetIdentified<MQMaterial>( doc, id );
423  #endif
424  }
425 
427  std::string GetMaterialTextureName( const MQMaterial, DWORD );
429 
431  inline std::string GetMaterialAlphaName( const MQMaterial mat ) { return GetMaterialTextureName(mat,MQMAPPING_ALPHA); }
433  inline std::string GetMaterialBumpName( const MQMaterial mat ) { return GetMaterialTextureName(mat,MQMAPPING_BUMP); }
436  inline int GetMaterialIndex( const MQDocument doc, const char* name ) { return prv_impl::GetNamedIndex<MQMaterial>( doc, name ); }
439  inline int GetMaterialIndex( const MQDocument doc, const UINT id ) { return prv_impl::GetIdentifiedIndex<MQMaterial>( doc, id ); }
449  inline std::string GetMaterialTextureName( const MQMaterial mat, const DWORD map_type = MQMAPPING_TEXTURE ) {
450  if ( mat != NULL )
451  {
452  std::vector<char> buf;
453  prv_impl::GetMaterialTextureName(&buf, mat, map_type);
454  return std::string(&buf[0]);
455  }
456  return "";
457  }
460  inline std::string GetName( const MQMaterial mat ){ return prv_impl::GetName( mat ); }
463  inline std::string GetName( const MQObject obj ){ return prv_impl::GetName( obj ); }
466  inline MQObject GetObject( const MQDocument doc, const char* name ) { return prv_impl::GetNamed<MQObject>( doc, name ); }
471  inline MQObject GetObject( const MQDocument doc, const UINT id ) {
472  #if 0x0310 <= MQPLUGIN_VERSION
473  if ( doc != NULL ) return doc->GetObjectFromUniqueID(id);
474  return NULL;
475  #else
476  return prv_impl::GetIdentified<MQObject>(doc, id );
477  #endif
478  }
481  inline int GetObjectIndex( const MQDocument doc, const char* name ) { return prv_impl::GetNamedIndex<MQObject>( doc, name ); }
484  inline int GetObjectIndex( const MQDocument doc, const UINT id ) { return prv_impl::GetIdentifiedIndex<MQObject>(doc, id ); }
485 
486 #if 0x0310 <= MQPLUGIN_VERSION
487 
491  inline std::string GetUnusedMaterialName( const MQDocument doc, const char* base_name = NULL ) {
492  return prv_impl::GetUnusedNameString<MQMaterial>( doc, base_name );
493  }
498  inline std::string GetUnusedMaterialName( const MQDocument doc, const MQMaterial base_name_material ) {
499  if ( base_name_material == NULL ) return prv_impl::GetUnusedNameString<MQMaterial>( doc );
500  return prv_impl::GetUnusedNameString<MQMaterial>( doc, GetName( base_name_material ).c_str() );
501  }
505  inline std::string GetUnusedObjectName( const MQDocument doc, const char* base_name = NULL ) {
506  return prv_impl::GetUnusedNameString<MQObject>( doc, base_name );
507  }
512  inline std::string GetUnusedObjectName( const MQDocument doc, const MQObject base_name_object ) {
513  if ( base_name_object == NULL ) return prv_impl::GetUnusedNameString<MQMaterial>( doc );
514  return prv_impl::GetUnusedNameString<MQMaterial>( doc, GetName( base_name_object ).c_str() );
515  }
516 
517 #endif
518 
520 
522 
523  namespace prv_impl {}; // kill prv_impl access ( to private header implement trap )
524 };
525 
526 #endif