Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tunit.h"
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
#include <math.h></math.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// Note: convertTo() means: convert from standard unit TO specific unit
Toshihiro Shimizu 890ddd
// e.g. if unit=cm (standard=inch) then convertTo(1) == 2.54
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Shinya Kitaoka d1f6c4
class VerticalCameraFldUnitConverter final : public TUnitConverter {
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
  static double m_factor;
Toshihiro Shimizu 890ddd
  VerticalCameraFldUnitConverter() {}
Shinya Kitaoka 120a6e
  TUnitConverter * clone() const {return new
Shinya Kitaoka 120a6e
VerticalCameraFldUnitConverter(*this);}
Toshihiro Shimizu 890ddd
  double convertTo(double v) const {return v*m_factor;}
Toshihiro Shimizu 890ddd
  double convertFrom(double v) const {return v/m_factor;}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
double VerticalCameraFldUnitConverter::m_factor = 1;
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace UnitParameters {
Toshihiro Shimizu 890ddd
Campbell Barton 8c6c57
static std::pair<double, double=""> dummyCurrentDpiGetter() {</double,>
Shinya Kitaoka 120a6e
  return std::make_pair<double, double="">(72, 72);</double,>
Shinya Kitaoka 120a6e
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
CurrentDpiGetter currentDpiGetter = &dummyCurrentDpiGetter;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void setCurrentDpiGetter(CurrentDpiGetter f) { currentDpiGetter = f; }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class VerticalFldUnitConverter final : public TUnitConverter {
Shinya Kitaoka 120a6e
  double m_factor;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  static double m_fieldGuideAspectRatio;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  VerticalFldUnitConverter(double factor) : m_factor(factor) {}
Shinya Kitaoka 38fd86
  TUnitConverter *clone() const override {
Shinya Kitaoka 38fd86
    return new VerticalFldUnitConverter(*this);
Shinya Kitaoka 38fd86
  }
Shinya Kitaoka 473e70
  double convertTo(double v) const override {
Shinya Kitaoka 120a6e
    return v * m_fieldGuideAspectRatio * m_factor;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 473e70
  double convertFrom(double v) const override {
Shinya Kitaoka 120a6e
    return v / (m_fieldGuideAspectRatio * m_factor);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
double VerticalFldUnitConverter::m_fieldGuideAspectRatio = 1.38;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace UnitParameters {
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void setFieldGuideAspectRatio(double ar) {
Shinya Kitaoka 120a6e
  assert(ar > 0);
Shinya Kitaoka 120a6e
  VerticalFldUnitConverter::m_fieldGuideAspectRatio = ar;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double getFieldGuideAspectRatio() {
Shinya Kitaoka 120a6e
  return VerticalFldUnitConverter::m_fieldGuideAspectRatio;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class TangentConverter final : public TUnitConverter {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TangentConverter() {}
Shinya Kitaoka 473e70
  TUnitConverter *clone() const override { return new TangentConverter(*this); }
Shinya Kitaoka 38fd86
  double convertTo(double v) const override {
Shinya Kitaoka 38fd86
    return atan(v) * (M_1_PI * 180.0);
Shinya Kitaoka 38fd86
  }
Shinya Kitaoka 473e70
  double convertFrom(double v) const override { return tan(v * M_PI_180); }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class TPixelUnitXConverter final : public TUnitConverter {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TPixelUnitXConverter() {}
Shinya Kitaoka 38fd86
  TUnitConverter *clone() const override {
Shinya Kitaoka 38fd86
    return new TPixelUnitXConverter(*this);
Shinya Kitaoka 38fd86
  }
Shinya Kitaoka 473e70
  double convertTo(double v) const override {
Shinya Kitaoka 120a6e
    return v * UnitParameters::currentDpiGetter().first;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 473e70
  double convertFrom(double v) const override {
Shinya Kitaoka 120a6e
    return v / UnitParameters::currentDpiGetter().first;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class TPixelUnitYConverter final : public TUnitConverter {
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  TPixelUnitYConverter() {}
Shinya Kitaoka 38fd86
  TUnitConverter *clone() const override {
Shinya Kitaoka 38fd86
    return new TPixelUnitYConverter(*this);
Shinya Kitaoka 38fd86
  }
Shinya Kitaoka 473e70
  double convertTo(double v) const override {
Shinya Kitaoka 120a6e
    return v * UnitParameters::currentDpiGetter().second;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 473e70
  double convertFrom(double v) const override {
Shinya Kitaoka 120a6e
    return v / UnitParameters::currentDpiGetter().second;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
TUnit::TUnit(std::wstring ext, TUnitConverter *converter)
Shinya Kitaoka 120a6e
    : m_defaultExtension(ext), m_converter(converter) {
Shinya Kitaoka 120a6e
  m_extensions.push_back(ext);
Shinya Kitaoka 120a6e
  if (m_converter == 0) m_converter = new TSimpleUnitConverter();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TUnit::TUnit(const TUnit &src)
Shinya Kitaoka 120a6e
    : m_defaultExtension(src.m_defaultExtension)
Shinya Kitaoka 120a6e
    , m_extensions(src.m_extensions)
Shinya Kitaoka 120a6e
    , m_converter(src.m_converter->clone()) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TUnit::~TUnit() { delete m_converter; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TUnit::addExtension(std::wstring ext) {
Shinya Kitaoka 120a6e
  if (std::find(m_extensions.begin(), m_extensions.end(), ext) ==
Shinya Kitaoka 120a6e
      m_extensions.end())
Shinya Kitaoka 120a6e
    m_extensions.push_back(ext);
Shinya Kitaoka 120a6e
  if (m_defaultExtension.empty()) m_defaultExtension = ext;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TUnit::isExtension(std::wstring ext) const {
Shinya Kitaoka 120a6e
  return std::find(m_extensions.begin(), m_extensions.end(), ext) !=
Shinya Kitaoka 120a6e
         m_extensions.end();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TUnit::setDefaultExtension(std::wstring ext) {
Shinya Kitaoka 120a6e
  if (!ext.empty() &&
Shinya Kitaoka 120a6e
      std::find(m_extensions.begin(), m_extensions.end(), ext) ==
Shinya Kitaoka 120a6e
          m_extensions.end())
Shinya Kitaoka 120a6e
    m_extensions.push_back(ext);
Shinya Kitaoka 120a6e
  m_defaultExtension = ext;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
TMeasure::TMeasure(std::string name, TUnit *mainUnit)
Shinya Kitaoka 120a6e
    : m_name(name)
Shinya Kitaoka 120a6e
    , m_mainUnit(0)
Shinya Kitaoka 120a6e
    , m_standardUnit(0)
Shinya Kitaoka 120a6e
    , m_currentUnit(0)
Shinya Kitaoka 120a6e
    , m_defaultValue(0) {
Shinya Kitaoka 120a6e
  add(mainUnit);
Shinya Kitaoka 120a6e
  m_mainUnit = m_currentUnit = m_standardUnit = mainUnit;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TMeasure::TMeasure(const TMeasure &src)
Shinya Kitaoka 120a6e
    : m_name(src.m_name)
Shinya Kitaoka 120a6e
    , m_mainUnit(src.m_mainUnit)
Shinya Kitaoka 120a6e
    , m_currentUnit(src.m_currentUnit)
Shinya Kitaoka 120a6e
    , m_standardUnit(src.m_standardUnit)
Shinya Kitaoka 120a6e
    , m_defaultValue(src.m_defaultValue) {
Shinya Kitaoka 120a6e
  std::map<std::wstring, *="" tunit="">::const_iterator it;</std::wstring,>
Shinya Kitaoka 120a6e
  for (it = src.m_extensions.begin(); it != src.m_extensions.end(); ++it) {
Shinya Kitaoka 120a6e
    TUnit *u = it->second;
Shinya Kitaoka 120a6e
    assert(u);
Shinya Kitaoka 120a6e
    const std::vector<std::wstring> &e = u->getExtensions();</std::wstring>
Shinya Kitaoka 120a6e
    assert(std::find(e.begin(), e.end(), it->first) != e.end());
Shinya Kitaoka 120a6e
    m_extensions[it->first] = u;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TMeasure::~TMeasure() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TMeasure::add(TUnit *unit) {
Shinya Kitaoka 120a6e
  const std::vector<std::wstring> &e = unit->getExtensions();</std::wstring>
Shinya Kitaoka 120a6e
  for (int i = 0; i < (int)e.size(); i++) {
Shinya Kitaoka 120a6e
    std::wstring ext = e[i];
Shinya Kitaoka 120a6e
    assert(m_extensions.count(ext) == 0);
Shinya Kitaoka 120a6e
    m_extensions[ext] = unit;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TUnit *TMeasure::getUnit(std::wstring ext) const {
Shinya Kitaoka 120a6e
  std::map<std::wstring, *="" tunit="">::const_iterator it;</std::wstring,>
Shinya Kitaoka 120a6e
  it = m_extensions.find(ext);
Shinya Kitaoka 120a6e
  return it == m_extensions.end() ? 0 : it->second;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TMeasure::setCurrentUnit(TUnit *unit) {
Shinya Kitaoka 120a6e
  assert(unit);
Shinya Kitaoka 120a6e
  assert(m_extensions.count(unit->getDefaultExtension()) > 0);
Shinya Kitaoka 120a6e
  m_currentUnit = unit;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TMeasure::setStandardUnit(TUnit *unit) {
Shinya Kitaoka 120a6e
  assert(unit);
Shinya Kitaoka 120a6e
  assert(m_extensions.count(unit->getDefaultExtension()) > 0);
Shinya Kitaoka 120a6e
  m_standardUnit = unit;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TMeasureManager::TMeasureManager() {
Shinya Kitaoka 120a6e
  TUnit inch(L"in"), cm(L"cm", new TSimpleUnitConverter(2.54)),
Shinya Kitaoka 120a6e
      mm(L"mm", new TSimpleUnitConverter(25.4)),
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      xfld(L"fld", new TSimpleUnitConverter(2)),
Shinya Kitaoka 120a6e
      cameraXFld(L"fld", new TSimpleUnitConverter(1)),
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      yfld(L"fld", new VerticalFldUnitConverter(2)),
Shinya Kitaoka 120a6e
      cameraYFld(L"fld", new VerticalFldUnitConverter(1)),
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      levelXFld(L"fld", new TSimpleUnitConverter(1)),
Shinya Kitaoka 120a6e
      levelYFld(L"fld", new VerticalFldUnitConverter(1)),
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      internalZDepth(L"internal.zdepth"),
Shinya Kitaoka 120a6e
      zdepth(L"zdepth", new TSimpleUnitConverter(1)), degree(L"\u00b0"),
Shinya Kitaoka 120a6e
      scale(L"*"), percentage2(L"%"), shear(L"sh"),
Shinya Kitaoka 120a6e
      shearAngle(L"\u00b0", new TangentConverter()),
Shinya Kitaoka 120a6e
      percentage(L"%", new TSimpleUnitConverter(100)),
Shinya Kitaoka 120a6e
      colorChannel(L"", new TSimpleUnitConverter(255)), dummy(L""),
Shinya Kitaoka 120a6e
      xPixel(L"px", new TPixelUnitXConverter()),
Shinya Kitaoka 120a6e
      yPixel(L"px", new TPixelUnitYConverter());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  inch.addExtension(L"inch");
Shinya Kitaoka 120a6e
  inch.addExtension(L"\"");
Shinya Kitaoka 120a6e
  inch.addExtension(L"''");
Shinya Kitaoka 120a6e
  inch.setDefaultExtension(L"\"");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  xPixel.addExtension(L"pixel");
Shinya Kitaoka 120a6e
  yPixel.addExtension(L"pixel");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  xfld.addExtension(L"field");
Shinya Kitaoka 120a6e
  cameraXFld.addExtension(L"field");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  yfld.addExtension(L"field");
Shinya Kitaoka 120a6e
  cameraYFld.addExtension(L"field");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  levelXFld.addExtension(L"field");
Shinya Kitaoka 120a6e
  levelYFld.addExtension(L"field");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  xfld.addExtension(L"F");
Shinya Kitaoka 120a6e
  yfld.addExtension(L"F");
Shinya Kitaoka 120a6e
  cameraXFld.addExtension(L"F");
Shinya Kitaoka 120a6e
  cameraYFld.addExtension(L"F");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  levelXFld.addExtension(L"F");
Shinya Kitaoka 120a6e
  levelYFld.addExtension(L"F");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TMeasure *length, *m;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  length = m = new TMeasure("length", inch.clone());
Shinya Kitaoka 120a6e
  m->add(cm.clone());
Shinya Kitaoka 120a6e
  /*---
Shinya Kitaoka 120a6e
  Fxの寸法パラメータは単位なし(実際にはStageInch(1 StageInch = 1/53.33333
Shinya Kitaoka 120a6e
  inch)という値)
Shinya Kitaoka 120a6e
  Fxの寸法パラメータからExpressionで単位のあるパラメータを参照すると、
Shinya Kitaoka 120a6e
  カレントUnitによってFxの計算結果が変わってしまう。
Shinya Kitaoka 120a6e
  tcomposerで用いられるカレントUnitはデフォルト値なので、
Shinya Kitaoka 120a6e
  ここでデフォルトのカレントUnitをmmにしておくことで、
Shinya Kitaoka 120a6e
  Unit = mm でシーンを作っておけば、作業時と同じRender結果が得られるようにする。
Shinya Kitaoka 120a6e
  ---*/
Shinya Kitaoka 120a6e
  TUnit *mmUnit = mm.clone();
Shinya Kitaoka 120a6e
  m->add(mmUnit);
Shinya Kitaoka 120a6e
  m->setCurrentUnit(mmUnit);
Shinya Kitaoka 120a6e
  add(m);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m = new TMeasure(*length);
Shinya Kitaoka 120a6e
  m->setName("length.x");
Shinya Kitaoka 120a6e
  m->add(xfld.clone());
Shinya Kitaoka 120a6e
  m->add(xPixel.clone());
Shinya Kitaoka 120a6e
  add(m);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m = new TMeasure(*length);
Shinya Kitaoka 120a6e
  m->setName("length.y");
Shinya Kitaoka 120a6e
  m->add(yfld.clone());
Shinya Kitaoka 120a6e
  m->add(yPixel.clone());
Shinya Kitaoka 120a6e
  add(m);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m = new TMeasure(*length);
Shinya Kitaoka 120a6e
  m->setName("length.lx");
Shinya Kitaoka 120a6e
  m->add(xfld.clone());
Shinya Kitaoka 120a6e
  m->add(xPixel.clone());
Shinya Kitaoka 120a6e
  add(m);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m = new TMeasure(*length);
Shinya Kitaoka 120a6e
  m->setName("length.ly");
Shinya Kitaoka 120a6e
  m->add(yfld.clone());
Shinya Kitaoka 120a6e
  m->add(yPixel.clone());
Shinya Kitaoka 120a6e
  add(m);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m = new TMeasure(*length);
Shinya Kitaoka 120a6e
  m->setName("camera.lx");
Shinya Kitaoka 120a6e
  m->add(cameraXFld.clone());
Shinya Kitaoka 120a6e
  m->add(xPixel.clone());
Shinya Kitaoka 120a6e
  add(m);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m = new TMeasure(*length);
Shinya Kitaoka 120a6e
  m->setName("camera.ly");
Shinya Kitaoka 120a6e
  m->add(cameraYFld.clone());
Shinya Kitaoka 120a6e
  m->add(yPixel.clone());
Shinya Kitaoka 120a6e
  add(m);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m = new TMeasure(*length);
Shinya Kitaoka 120a6e
  m->setName("level.lx");
Shinya Kitaoka 120a6e
  m->add(levelXFld.clone());
Shinya Kitaoka 120a6e
  add(m);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m = new TMeasure(*length);
Shinya Kitaoka 120a6e
  m->setName("level.ly");
Shinya Kitaoka 120a6e
  m->add(levelYFld.clone());
Shinya Kitaoka 120a6e
  add(m);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m = new TMeasure(*length);
Shinya Kitaoka 120a6e
  m->setName("canvas.lx");
Shinya Kitaoka 120a6e
  m->add(cameraXFld.clone());
Shinya Kitaoka 120a6e
  add(m);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m = new TMeasure(*length);
Shinya Kitaoka 120a6e
  m->setName("canvas.ly");
Shinya Kitaoka 120a6e
  m->add(cameraYFld.clone());
Shinya Kitaoka 120a6e
  add(m);
Shinya Kitaoka 120a6e
shun_iwasawa bb0d83
  const double stage_inch        = 53.33333;
shun_iwasawa d6df3f
  const double stage_standardDpi = 120;
Jeremy Bullock b99ed3
Shinya Kitaoka 120a6e
  TUnit fxLength(L"fxLength"),
Jeremy Bullock 59dbbf
      fxInch(L"in", new TSimpleUnitConverter(1 / stage_inch)),
Jeremy Bullock 59dbbf
      fxCm(L"cm", new TSimpleUnitConverter(2.54 / stage_inch)),
Jeremy Bullock 59dbbf
      fxMm(L"mm", new TSimpleUnitConverter(25.4 / stage_inch)),
Jeremy Bullock 59dbbf
      fxXfld(L"fld", new TSimpleUnitConverter(2 / stage_inch)),
shun_iwasawa d6df3f
      fxPx(L"px", new TSimpleUnitConverter(stage_standardDpi / stage_inch));
Shinya Kitaoka 120a6e
  fxInch.addExtension(L"inch");
Shinya Kitaoka 120a6e
  fxInch.addExtension(L"\"");
Shinya Kitaoka 120a6e
  fxInch.addExtension(L"''");
Shinya Kitaoka 120a6e
  fxInch.setDefaultExtension(L"\"");
Shinya Kitaoka 120a6e
  fxXfld.addExtension(L"field");
Shinya Kitaoka 120a6e
  fxXfld.addExtension(L"F");
Jeremy Bullock b6ada4
  fxPx.addExtension(L"pixel");
Shinya Kitaoka 120a6e
  m = new TMeasure("fxLength", fxLength.clone());
Shinya Kitaoka 120a6e
  m->add(fxInch.clone());
Shinya Kitaoka 120a6e
  m->add(fxCm.clone());
Shinya Kitaoka 120a6e
  m->add(fxMm.clone());
Shinya Kitaoka 120a6e
  m->add(fxXfld.clone());
Jeremy Bullock b99ed3
  m->add(fxPx.clone());
Shinya Kitaoka 120a6e
  add(m);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m = new TMeasure("angle", degree.clone());
Shinya Kitaoka 120a6e
  add(m);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m           = new TMeasure("scale", scale.clone());
Shinya Kitaoka 120a6e
  TUnit *unit = percentage.clone();
Toshihiro Shimizu 890ddd
  m->add(unit);
Toshihiro Shimizu 890ddd
  m->setCurrentUnit(unit);
Toshihiro Shimizu 890ddd
  m->setStandardUnit(unit);
Toshihiro Shimizu 890ddd
  add(m);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m    = new TMeasure("percentage", scale.clone());
Shinya Kitaoka 120a6e
  unit = percentage.clone();
Shinya Kitaoka 120a6e
  m->add(unit);
Shinya Kitaoka 120a6e
  m->setCurrentUnit(unit);
Shinya Kitaoka 120a6e
  m->setStandardUnit(unit);
Shinya Kitaoka 120a6e
  add(m);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m = new TMeasure("percentage2", percentage2.clone());
Shinya Kitaoka 120a6e
  add(m);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m    = new TMeasure("shear", shear.clone());
Shinya Kitaoka 120a6e
  unit = shearAngle.clone();
Shinya Kitaoka 120a6e
  m->add(unit);
Shinya Kitaoka 120a6e
  m->setCurrentUnit(unit);
Shinya Kitaoka 120a6e
  m->setStandardUnit(unit);
Shinya Kitaoka 120a6e
  add(m);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m = new TMeasure("colorChannel", colorChannel.clone());
Shinya Kitaoka 120a6e
  add(m);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*
Shinya Kitaoka 120a6e
m = new TMeasure("zdepth", internalZDepth.clone());
Shinya Kitaoka 120a6e
unit = zdepth.clone();
Shinya Kitaoka 120a6e
m->add(unit);
Shinya Kitaoka 120a6e
m->setCurrentUnit(unit);
Shinya Kitaoka 120a6e
m->setStandardUnit(unit);
Shinya Kitaoka 120a6e
add(m);
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
  m = new TMeasure("dummy", dummy.clone());
Shinya Kitaoka 120a6e
  add(m);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TMeasureManager::add(TMeasure *m) { m_measures[m->getName()] = m; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TMeasure *TMeasureManager::get(std::string name) const {
Shinya Kitaoka 120a6e
  std::map<std::string, *="" tmeasure="">::const_iterator it;</std::string,>
Shinya Kitaoka 120a6e
  it = m_measures.find(name);
Shinya Kitaoka 120a6e
  if (it == m_measures.end())
Shinya Kitaoka 120a6e
    return 0;
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return it->second;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
TMeasuredValue::TMeasuredValue(std::string measureName)
Shinya Kitaoka 120a6e
    : m_measure(0), m_value(0) {
Shinya Kitaoka 120a6e
  setMeasure(measureName);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TMeasuredValue::~TMeasuredValue() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TMeasuredValue::setMeasure(const TMeasure *measure) {
Shinya Kitaoka 120a6e
  assert(measure);
Shinya Kitaoka 120a6e
  if (!measure) return;
Shinya Kitaoka 120a6e
  m_measure = measure;
Shinya Kitaoka 120a6e
  m_value   = m_measure->getDefaultValue();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TMeasuredValue::setMeasure(std::string measureName) {
Shinya Kitaoka 120a6e
  setMeasure(TMeasureManager::instance()->get(measureName));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool TMeasuredValue::setValue(std::wstring s, int *pErr) {
Shinya Kitaoka 120a6e
  if (s == L"") {
Shinya Kitaoka 120a6e
    if (pErr) *pErr = -1;
Shinya Kitaoka 120a6e
    return false;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  const TUnit *unit = m_measure->getCurrentUnit();
Shinya Kitaoka 120a6e
  double value      = 0;
Shinya Kitaoka 120a6e
  bool valueFlag    = false;
Shinya Kitaoka 120a6e
  int i = 0, len = s.length();
Shinya Kitaoka 120a6e
  // skip blanks
Shinya Kitaoka 120a6e
  i = s.find_first_not_of(::to_wstring(" \t"));
Shinya Kitaoka 120a6e
  assert(i != (int)std::wstring::npos);
Shinya Kitaoka 120a6e
  int j = i;
Shinya Kitaoka 120a6e
  // match number
Shinya Kitaoka 120a6e
  if (i < len && (s[i] == L'-' || s[i] == L'+')) i++;
Shinya Kitaoka 120a6e
  while (i < len && L'0' <= s[i] && s[i] <= L'9') i++;
Shinya Kitaoka 120a6e
  if (i < len && s[i] == L'.') {
Shinya Kitaoka 120a6e
    i++;
Shinya Kitaoka 120a6e
    while (i < len && L'0' <= s[i] && s[i] <= L'9') i++;
Shinya Kitaoka 120a6e
    if (i < len && (s[i] == L'E' || s[i] == L'e')) {
Shinya Kitaoka 120a6e
      i++;
Shinya Kitaoka 120a6e
      if (i < len && (s[i] == L'-' || s[i] == L'+')) i++;
Shinya Kitaoka 120a6e
      while (i < len && L'0' <= s[i] && s[i] <= L'9') i++;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (i > j) {
Shinya Kitaoka 120a6e
    value     = std::stod(s.substr(j, i - j));
Shinya Kitaoka 120a6e
    valueFlag = true;
Shinya Kitaoka 120a6e
    // skip blanks
Shinya Kitaoka 120a6e
    i = s.find_first_not_of(::to_wstring(" \t"), i);
Shinya Kitaoka 120a6e
    if (i == (int)std::wstring::npos) i = s.length();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // remove trailing blanks
Shinya Kitaoka 120a6e
  if (i < (int)s.length()) {
Shinya Kitaoka 120a6e
    j = i;
Shinya Kitaoka 120a6e
    i = s.find_last_not_of(::to_wstring(" \t"));
Shinya Kitaoka 120a6e
    if (i == (int)std::wstring::npos) i = len - 1;
Shinya Kitaoka 120a6e
    if (j <= i) {
Shinya Kitaoka 120a6e
      std::wstring unitExt = s.substr(j, i + 1 - j);
Shinya Kitaoka 120a6e
      unit                 = m_measure->getUnit(unitExt);
Shinya Kitaoka 120a6e
      if (!unit) {
Shinya Kitaoka 120a6e
        if (pErr) *pErr = -2;
Shinya Kitaoka 120a6e
        return false;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (valueFlag) {
Shinya Kitaoka 120a6e
    double newValue = unit->convertFrom(value);
Shinya Kitaoka 120a6e
    if (m_value == newValue) {
Shinya Kitaoka 120a6e
      if (pErr) *pErr = 0;
Shinya Kitaoka 120a6e
      return false;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    m_value = newValue;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (pErr) *pErr = (valueFlag || unit != 0) ? 0 : -3;
Shinya Kitaoka 120a6e
  return valueFlag || unit != 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
std::wstring TMeasuredValue::toWideString(int decimals) const {
Shinya Kitaoka 120a6e
  double v      = getValue(CurrentUnit);
Shinya Kitaoka 120a6e
  std::string s = ::to_string(v, decimals);
Shinya Kitaoka 120a6e
  if (s.find('.') != std::string::npos) {
Shinya Kitaoka 120a6e
    int i = s.length();
Shinya Kitaoka 120a6e
    while (i > 0 && s[i - 1] == '0') i--;
Shinya Kitaoka 120a6e
    if (i > 0 && s[i - 1] == '.') i--;
Shinya Kitaoka 120a6e
    if (i < (int)s.length()) s = s.substr(0, i);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  std::wstring measure = m_measure->getCurrentUnit()->getDefaultExtension();
Shinya Kitaoka 120a6e
  if (measure.empty()) return ::to_wstring(s);
Shinya Kitaoka 120a6e
  return ::to_wstring(s) + L" " + measure;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class ZDepthUnitConverter final : public TUnitConverter {
Shinya Kitaoka 120a6e
  TMeasureManager::CameraSizeProvider *m_cameraSizeProvider;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  ZDepthUnitConverter(TMeasureManager::CameraSizeProvider *cameraSizeProvider)
Shinya Kitaoka 120a6e
      : m_cameraSizeProvider(cameraSizeProvider) {}
Shinya Kitaoka 473e70
  TUnitConverter *clone() const override {
Shinya Kitaoka 120a6e
    return new ZDepthUnitConverter(m_cameraSizeProvider);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  inline double getCameraSize() const { return (*m_cameraSizeProvider)(); }
Shinya Kitaoka 38fd86
  double convertTo(double v) const override {
Shinya Kitaoka 38fd86
    return (1 - v * 0.001) * getCameraSize();
Shinya Kitaoka 38fd86
  }
Shinya Kitaoka 473e70
  double convertFrom(double v) const override {
Shinya Kitaoka 120a6e
    return (1 - v / getCameraSize()) * 1000.0;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class CameraZDepthUnitConverter final : public TUnitConverter {
Shinya Kitaoka 120a6e
  TMeasureManager::CameraSizeProvider *m_cameraSizeProvider;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  CameraZDepthUnitConverter(
Shinya Kitaoka 120a6e
      TMeasureManager::CameraSizeProvider *cameraSizeProvider)
Shinya Kitaoka 120a6e
      : m_cameraSizeProvider(cameraSizeProvider) {}
Shinya Kitaoka 473e70
  TUnitConverter *clone() const override {
Shinya Kitaoka 120a6e
    return new CameraZDepthUnitConverter(m_cameraSizeProvider);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  inline double getCameraSize() const { return (*m_cameraSizeProvider)(); }
Shinya Kitaoka 38fd86
  double convertTo(double v) const override {
Shinya Kitaoka 38fd86
    return (1 + v * 0.001) * getCameraSize();
Shinya Kitaoka 38fd86
  }
Shinya Kitaoka 473e70
  double convertFrom(double v) const override {
Shinya Kitaoka 120a6e
    return (v / getCameraSize() - 1) * 1000.0;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
/*-- Zのカーブのハンドルの長さは0=0となるようにしなければならない --*/
Shinya Kitaoka d1f6c4
class ZDepthHandleUnitConverter final : public TUnitConverter {
Shinya Kitaoka 120a6e
  TMeasureManager::CameraSizeProvider *m_cameraSizeProvider;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  ZDepthHandleUnitConverter(
Shinya Kitaoka 120a6e
      TMeasureManager::CameraSizeProvider *cameraSizeProvider)
Shinya Kitaoka 120a6e
      : m_cameraSizeProvider(cameraSizeProvider) {}
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  TUnitConverter *clone() const override {
Shinya Kitaoka 120a6e
    return new ZDepthHandleUnitConverter(m_cameraSizeProvider);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  inline double getCameraSize() const { return (*m_cameraSizeProvider)(); }
Shinya Kitaoka 38fd86
  double convertTo(double v) const override {
Shinya Kitaoka 38fd86
    return -v * 0.001 * getCameraSize();
Shinya Kitaoka 38fd86
  }
Shinya Kitaoka 38fd86
  double convertFrom(double v) const override {
Shinya Kitaoka 38fd86
    return (-v / getCameraSize()) * 1000.0;
Shinya Kitaoka 38fd86
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class CameraZDepthHandleUnitConverter final : public TUnitConverter {
Shinya Kitaoka 120a6e
  TMeasureManager::CameraSizeProvider *m_cameraSizeProvider;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  CameraZDepthHandleUnitConverter(
Shinya Kitaoka 120a6e
      TMeasureManager::CameraSizeProvider *cameraSizeProvider)
Shinya Kitaoka 120a6e
      : m_cameraSizeProvider(cameraSizeProvider) {}
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  TUnitConverter *clone() const override {
Shinya Kitaoka 120a6e
    return new CameraZDepthHandleUnitConverter(m_cameraSizeProvider);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  inline double getCameraSize() const { return (*m_cameraSizeProvider)(); }
Shinya Kitaoka 38fd86
  double convertTo(double v) const override {
Shinya Kitaoka 38fd86
    return v * 0.001 * getCameraSize();
Shinya Kitaoka 38fd86
  }
Shinya Kitaoka 38fd86
  double convertFrom(double v) const override {
Shinya Kitaoka 38fd86
    return (v / getCameraSize()) * 1000.0;
Shinya Kitaoka 38fd86
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TMeasureManager::addCameraMeasures(
Shinya Kitaoka 120a6e
    CameraSizeProvider *cameraSizeProvider) {
Shinya Kitaoka 120a6e
  TUnit u0(L"z"), u1(L"fld", new ZDepthUnitConverter(cameraSizeProvider)),
Shinya Kitaoka 120a6e
      u2(L"fld", new CameraZDepthUnitConverter(cameraSizeProvider)),
Shinya Kitaoka 120a6e
      u3(L"fld", new ZDepthHandleUnitConverter(cameraSizeProvider)),
Shinya Kitaoka 120a6e
      u4(L"fld", new CameraZDepthHandleUnitConverter(cameraSizeProvider));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TMeasure *zdepth = new TMeasure("zdepth", u0.clone());
Shinya Kitaoka 120a6e
  TUnit *u         = u1.clone();
Shinya Kitaoka 120a6e
  zdepth->add(u);
Shinya Kitaoka 120a6e
  zdepth->setCurrentUnit(u);
Shinya Kitaoka 120a6e
  zdepth->setStandardUnit(u);
Shinya Kitaoka 120a6e
  TMeasureManager::instance()->add(zdepth);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  zdepth = new TMeasure("zdepth.cam", u0.clone());
Shinya Kitaoka 120a6e
  u      = u2.clone();
Shinya Kitaoka 120a6e
  zdepth->add(u);
Shinya Kitaoka 120a6e
  zdepth->setCurrentUnit(u);
Shinya Kitaoka 120a6e
  zdepth->setStandardUnit(u);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TMeasureManager::instance()->add(zdepth);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  zdepth = new TMeasure("zdepth.handle", u0.clone());
Shinya Kitaoka 120a6e
  u      = u3.clone();
Shinya Kitaoka 120a6e
  zdepth->add(u);
Shinya Kitaoka 120a6e
  zdepth->setCurrentUnit(u);
Shinya Kitaoka 120a6e
  zdepth->setStandardUnit(u);
Shinya Kitaoka 120a6e
  TMeasureManager::instance()->add(zdepth);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  zdepth = new TMeasure("zdepth.cam.handle", u0.clone());
Shinya Kitaoka 120a6e
  u      = u4.clone();
Shinya Kitaoka 120a6e
  zdepth->add(u);
Shinya Kitaoka 120a6e
  zdepth->setCurrentUnit(u);
Shinya Kitaoka 120a6e
  zdepth->setStandardUnit(u);
Shinya Kitaoka 120a6e
  TMeasureManager::instance()->add(zdepth);
Toshihiro Shimizu 890ddd
}