da847a
da847a
da847a
// TnzTools includes
da847a
#include <tools replicator.h=""></tools>
da847a
#include <tools modifierclone.h="" modifiers=""></tools>
da847a
#include <tools assistants="" guidelineline.h=""></tools>
da847a
da847a
da847a
// TnzCore includes
da847a
#include <tgl.h></tgl.h>
da847a
da847a
da847a
//*****************************************************************************************
da847a
//    TReplicatorMirror implementation
da847a
//*****************************************************************************************
da847a
da847a
class TReplicatorMirror final : public TReplicator {
da847a
  Q_DECLARE_TR_FUNCTIONS(TReplicatorMirror)
da847a
public:
da847a
  const TStringId m_idDiscreteAngle;
da847a
  const TStringId m_idPressure;
da847a
da847a
protected:
da847a
  TAssistantPoint &m_a;
da847a
  TAssistantPoint &m_b;
da847a
da847a
public:
da847a
  TReplicatorMirror(TMetaObject &object):
da847a
    TReplicator(object),
da847a
    m_idDiscreteAngle("discreteAngle"),
da847a
    m_idPressure("pressure"),
da847a
    m_a( addPoint("a", TAssistantPoint::CircleCross) ),
da847a
    m_b( addPoint("b", TAssistantPoint::Circle, TPointD(0, -50)) )
da847a
  {
da847a
    addProperty( new TBoolProperty(m_idDiscreteAngle.str(),  getDiscreteAngle()) );
da847a
    addProperty( new TDoubleProperty(m_idPressure.str(), 0.0, 2.0, getPressure()) );
da847a
  }
da847a
da847a
  
da847a
  static QString getLocalName()
da847a
    { return tr("Replicator Mirror"); }
da847a
da847a
    
da847a
  void updateTranslation() const override {
da847a
    TReplicator::updateTranslation();
da847a
    setTranslation(m_idDiscreteAngle, tr("Discrete Angle"));
da847a
    setTranslation(m_idPressure, tr("Pressure"));
da847a
  }
da847a
da847a
  
da847a
  inline bool getDiscreteAngle() const
da847a
    { return data()[m_idDiscreteAngle].getBool(); }
da847a
  inline double getPressure() const
da847a
    { return data()[m_idPressure].getDouble(); }
da847a
da847a
protected:
da847a
  inline void setPressure(double x)
da847a
    { if (getPressure() != x) data()[m_idPressure].setDouble(x); }
da847a
da847a
    
da847a
  void onSetDefaults() override {
da847a
    setPressure(1);
da847a
    TReplicator::onSetDefaults();
da847a
  }
da847a
da847a
  
da847a
  void onFixData() override {
da847a
    TReplicator::onFixData();
da847a
    setPressure( std::max(0.0, std::min(2.0, getPressure())) );
da847a
  }
da847a
da847a
da847a
  TPointD fixB() const {
da847a
    TPointD b = m_b.position;
da847a
    
da847a
    if (getDiscreteAngle()) {
da847a
      TPointD d = b - m_a.position;
da847a
      double l = norm2(d);
da847a
      if (l > TConsts::epsilon*TConsts::epsilon) {
da847a
        l = sqrt(l);
da847a
        double angle = atan2(d.y, d.x);
da847a
        angle = round(angle*4/M_PI)*M_PI/4;
da847a
        b.x = cos(angle)*l + m_a.position.x;
da847a
        b.y = sin(angle)*l + m_a.position.y;
da847a
      }
da847a
    }
da847a
    
da847a
    if (areAlmostEqual(b, m_a.position))
da847a
      b = m_a.position + TPointD(1, 0);
da847a
    
da847a
    return b;
da847a
  }
da847a
  
da847a
  
da847a
  TAffine getAffine(const TAffine &toTool = TAffine()) const {
da847a
    TPointD c = toTool*m_a.position;
da847a
    TPointD x = toTool*fixB() - c;
da847a
    TPointD y(-x.y, x.x);
da847a
    
da847a
    TAffine t0( x.x,  y.x, c.x,
da847a
                x.y,  y.y, c.y );
da847a
    TAffine t1( x.x, -y.x, c.x,
da847a
                x.y, -y.y, c.y );
da847a
    return t1*t0.inv();
da847a
  }
da847a
  
da847a
da847a
  void onMovePoint(TAssistantPoint &point, const TPointD &position) override {
da847a
    if (&point == &m_a)
da847a
      m_b.position += position - m_a.position;
da847a
    point.position = position;
da847a
  }
da847a
  
da847a
da847a
public:
da847a
  int getMultipler() const override
da847a
    { return 2; }
da847a
  
da847a
  
da847a
  void getPoints(const TAffine &toTool, PointList &points) const override
da847a
    { transformPoints(getAffine(toTool), points, (int)points.size()); }
da847a
  
da847a
  
da847a
  void getModifiers(
da847a
    const TAffine &toTool,
da847a
    TInputModifier::List &outModifiers ) const override
da847a
  {
da847a
    TModifierClone *modifier = new TModifierClone();
da847a
    modifier->transforms.push_back(TTrackTransform(
da847a
      getAffine(toTool), getPressure() ));
da847a
    outModifiers.push_back(modifier);
da847a
  }
da847a
da847a
  
da847a
  void draw(TToolViewer*, bool enabled) const override {
da847a
    TPointD p0 = m_a.position;
da847a
    TPointD p1 = fixB();
da847a
    
da847a
    TAffine4 modelview, projection;
da847a
    glGetDoublev(GL_MODELVIEW_MATRIX, modelview.a);
da847a
    glGetDoublev(GL_PROJECTION_MATRIX, projection.a);
da847a
da847a
    TAffine matrix = (projection*modelview).get2d();
da847a
    TPointD pp0 = matrix*p0;
da847a
    TPointD pp1 = matrix*p1;
da847a
    const TRectD oneBox(-1.0, -1.0, 1.0, 1.0);
da847a
    if (!TGuidelineLine::truncateInfiniteLine(oneBox, pp0, pp1)) return;
da847a
da847a
    double alpha = getDrawingAlpha(enabled);
da847a
    double pixelSize = sqrt(tglGetPixelSize2());
da847a
    TAffine matrixInv = matrix.inv();
da847a
    drawSegment(matrixInv*pp0, matrixInv*pp1, pixelSize, alpha);
da847a
  }
da847a
};
da847a
da847a
da847a
//*****************************************************************************************
da847a
//    Registration
da847a
//*****************************************************************************************
da847a
da847a
static TAssistantTypeT<treplicatormirror> replicatorMirror("replicatorMirror");</treplicatormirror>