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
//    TReplicatorStar implementation
da847a
//*****************************************************************************************
da847a
da847a
class TReplicatorStar final : public TReplicator {
da847a
  Q_DECLARE_TR_FUNCTIONS(TReplicatorStar)
da847a
public:
da847a
  const TStringId m_idDiscreteAngle;
da847a
  const TStringId m_idMirror;
da847a
  const TStringId m_idCount;
da847a
da847a
protected:
da847a
  TAssistantPoint &m_center;
da847a
  TAssistantPoint &m_a;
da847a
da847a
public:
da847a
  TReplicatorStar(TMetaObject &object):
da847a
    TReplicator(object),
da847a
    m_idDiscreteAngle("discreteAngle"),
da847a
    m_idMirror("mirror"),
da847a
    m_idCount("count"),
da847a
    m_center( addPoint("center", TAssistantPoint::CircleCross) ),
da847a
    m_a     ( addPoint("a",      TAssistantPoint::Circle, TPointD(80, 0)) )
da847a
  {
da847a
    addProperty( new TBoolProperty(m_idDiscreteAngle.str(), getDiscreteAngle()) );
da847a
    addProperty( new TBoolProperty(m_idMirror.str(),        getMirror()) );
da847a
    addProperty( createCountProperty(m_idCount, getCount(), 2) );
da847a
  }
da847a
da847a
  
da847a
  static QString getLocalName()
da847a
    { return tr("Replicator Star"); }
da847a
da847a
    
da847a
  void updateTranslation() const override {
da847a
    TReplicator::updateTranslation();
da847a
    setTranslation(m_idDiscreteAngle, tr("Discrete Angle"));
da847a
    setTranslation(m_idMirror, tr("Mirror"));
da847a
    setTranslation(m_idCount, tr("Count"));
da847a
  }
da847a
da847a
  
da847a
  inline bool getDiscreteAngle() const
da847a
    { return data()[m_idDiscreteAngle].getBool(); }
da847a
  inline bool getMirror() const
da847a
    { return data()[m_idMirror].getBool(); }
da847a
  inline int getCount() const
da847a
    { return (int)data()[m_idCount].getDouble(); }
da847a
da847a
protected:
da847a
  inline void setCount(int x)
da847a
    { if (getCount() != (double)x) data()[m_idCount].setDouble((double)x); }
da847a
da847a
    
da847a
  void onSetDefaults() override {
da847a
    setCount(6);
da847a
    TReplicator::onSetDefaults();
da847a
  }
da847a
da847a
  
da847a
  void onFixData() override {
da847a
    TReplicator::onFixData();
da847a
    setCount( std::max(1, std::min(multiplierSoftLimit - 1, getCount())) );
da847a
  }
da847a
da847a
da847a
  TPointD fixA() const {
da847a
    TPointD a = m_a.position;
da847a
    
da847a
    if (getDiscreteAngle()) {
da847a
      TPointD d = a - m_center.position;
da847a
      double l = norm2(d);
da847a
      if (l > TConsts::epsilon*TConsts::epsilon) {
da847a
        l = sqrt(l);
da847a
        int count = getCount();
da847a
        if (count > 0) {
da847a
          double angle = atan2(d.y, d.x);
da847a
          angle = round(angle*2*count/M_PI)*M_PI/(2*count);
da847a
          a.x = cos(angle)*l + m_center.position.x;
da847a
          a.y = sin(angle)*l + m_center.position.y;
da847a
        }
da847a
      }
da847a
    }
da847a
    
da847a
    if (areAlmostEqual(a, m_center.position))
da847a
      a = m_center.position + TPointD(1, 0);
da847a
    
da847a
    return a;
da847a
  }
da847a
da847a
  
da847a
  void onMovePoint(TAssistantPoint &point, const TPointD &position) override {
da847a
    if (&point == &m_center)
da847a
      m_a.position += position - m_center.position;
da847a
    point.position = position;
da847a
  }
da847a
  
da847a
  
da847a
public:
da847a
  int getMultipler() const override
da847a
    { return getCount() + 1; }
da847a
  
da847a
  
da847a
  void getPoints(const TAffine &toTool, PointList &points) const override {
da847a
    points.reserve(points.size() * getMultipler());
da847a
    int pointsCount = (int)points.size();
da847a
    
da847a
    int count = getCount();
da847a
    bool mirror = getMirror();
da847a
da847a
    TPointD c = toTool*m_center.position;
da847a
    TPointD x = toTool*fixA() - c;
da847a
    TPointD y(-x.y, x.x);
da847a
da847a
    TAffine t1( x.x,  y.x, c.x,
da847a
                x.y,  y.y, c.y );
da847a
    TAffine t2( x.x, -y.x, c.x,
da847a
                x.y, -y.y, c.y );
da847a
    
da847a
    TAffine t0 = t1.inv();
da847a
    TRotation r(360.0/getCount());
da847a
                          
da847a
    for(int i = 0; i < count; ++i) {
da847a
      if (i)
da847a
        transformPoints(t1*t0, points, pointsCount);
da847a
      if (mirror) {
da847a
        transformPoints(t2*t0, points, pointsCount);
da847a
        t2 *= r;
da847a
      }
da847a
      t1 *= r;
da847a
    }
da847a
  }
da847a
  
da847a
  
da847a
  void getModifiers(
da847a
    const TAffine &toTool,
da847a
    TInputModifier::List &outModifiers ) const override
da847a
  {
da847a
    int count = getCount();
da847a
    bool mirror = getMirror();
da847a
da847a
    TPointD c = toTool*m_center.position;
da847a
    TPointD x = toTool*fixA() - c;
da847a
    TPointD y(-x.y, x.x);
da847a
da847a
    TAffine t1( x.x,  y.x, c.x,
da847a
                x.y,  y.y, c.y );
da847a
    TAffine t2( x.x, -y.x, c.x,
da847a
                x.y, -y.y, c.y );
da847a
    
da847a
    TAffine t0 = t1.inv();
da847a
    TRotation r(360.0/getCount());
da847a
                          
da847a
    TModifierClone *modifier = new TModifierClone();
da847a
    for(int i = 0; i < count; ++i) {
da847a
      if (i)
da847a
        modifier->transforms.push_back(TTrackTransform(t1*t0));
da847a
      if (mirror) {
da847a
        modifier->transforms.push_back(TTrackTransform(t2*t0));
da847a
        t2 *= r;
da847a
      }
da847a
      t1 *= r;
da847a
    }
da847a
    
da847a
    outModifiers.push_back(modifier);
da847a
  }
da847a
da847a
  
da847a
  void draw(TToolViewer*, bool enabled) const override {
da847a
    double alpha = getDrawingAlpha(enabled);
da847a
    double gridAlpha = getDrawingGridAlpha();
da847a
    double pixelSize = sqrt(tglGetPixelSize2());
da847a
da847a
    int count = getCount();
da847a
    bool mirror = getMirror();
da847a
da847a
    TPointD c = m_center.position;
da847a
    TPointD a = fixA();
da847a
    TPointD d = normalize(a - c);
da847a
    
da847a
    double spacing = 10*pixelSize;
da847a
    double l = spacing*count/M_2PI;
da847a
    
da847a
    TPointD p0 = c + d*l;
da847a
    TPointD p1 = p0 + d;
da847a
    TRotation r(360.0/count);
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
    TAffine matrixInv = matrix.inv();
da847a
    const TRectD oneBox(-1.0, -1.0, 1.0, 1.0);
da847a
    
da847a
    for(int i = 0; i < count; ++i) {
da847a
      TPointD pp0 = matrix*p0;
da847a
      TPointD pp1 = matrix*p1;
da847a
      if (TGuidelineLine::truncateRay(oneBox, pp0, pp1))
da847a
        drawSegment(matrixInv*pp0, matrixInv*pp1, pixelSize, i ? gridAlpha : alpha);
da847a
      p0 = r*(p0 - c) + c;
da847a
      p1 = r*(p1 - c) + c;
da847a
    }
da847a
    
da847a
    TPointD p = TPointD(-d.y, d.x);
da847a
    drawSegment( (mirror ? a+p*15 : a), a-p*15, pixelSize, alpha );
da847a
    
da847a
    drawSegment(c - d*10, c + d*10, pixelSize, alpha);
da847a
    drawSegment(c - p*10, c + p*10, pixelSize, alpha);
da847a
  }
da847a
};
da847a
da847a
da847a
//*****************************************************************************************
da847a
//    Registration
da847a
//*****************************************************************************************
da847a
da847a
static TAssistantTypeT<treplicatorstar> replicatorStar("replicatorStar");</treplicatorstar>