~vbursian/research-assistant/intervers

« back to all changes in this revision

Viewing changes to RANet/Curves.cpp

  • Committer: Viktor Bursian
  • Date: 2013-06-06 15:10:08 UTC
  • Revision ID: vbursian@gmail.com-20130606151008-6641eh62f0lgx8jt
Tags: version_0.3.0
version 0.3.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
                     Viktor.Bursian@mail.ioffe.ru
6
6
*///////////////////////////////////////////////////////////////////////////////
7
7
#include "Curves.h"
 
8
#include "Symbols.h"
8
9
#include "Excepts.h"
 
10
#include "Log.h"
9
11
#include <math.h>
10
12
#include <limits>
11
13
namespace RA {
13
15
 
14
16
DEFINE_CLASS_TAG(sCurveAppearance)
15
17
DEFINE_CLASS_TAG(sCurve)
 
18
DEFINE_CLASS_TAG(sFunction)
 
19
DEFINE_CLASS_TAG(sDependence)
16
20
 
17
21
//--------------------------------------------------------- sCurveAppearance ---
18
22
 
37
41
  Line.Store(S);
38
42
};
39
43
 
40
 
 
41
44
//------------------------------------------------------------------- sCurve ---
42
45
 
43
46
sCurve::sCurve (rsObjectStream a_stream)
44
 
    :s2DVisibleObject(a_stream)
 
47
    :sGraphObject(a_stream)
 
48
{
 
49
};
 
50
 
 
51
 
 
52
void  sCurve::Store (rsObjectStream a_stream)
 
53
{
 
54
  sGraphObject::Store(a_stream);
 
55
};
 
56
 
 
57
 
 
58
int  sCurve::Distance (rcsIntPoint  position
 
59
                      ,rcsScales    scales  )
 
60
{
 
61
 
 
62
 
 
63
  sCurve::psTracer            T = GetCurveTracer(scales);
 
64
  int                         D_min = std::numeric_limits<int>::max();
 
65
 
 
66
                                  //  P -- точка с координатами position
 
67
  sPoint                      R;  //вектор, идущий из точки кривой в точку P
 
68
  sPoint                      L;       //вектор отрезка кривой
 
69
  real                        LengthL; //его длина
 
70
  sPoint                      PP;  //вектор, идущий из начала отрезка кривой
 
71
                                   //в точку P
 
72
  sPoint                      PPP; //координаты точки P в системе координат,
 
73
                                   //повёрнутой так, что ось х совмещена с
 
74
                                   //с вектором L
 
75
  real                        DD_min ,DD;
 
76
  DD_min= real(std::numeric_limits<int>::max())
 
77
        * real(std::numeric_limits<int>::max());
 
78
  while( T->NextPoint() ){
 
79
    R = position - scales.Tr(T->Point);
 
80
    DD=R.X*R.X+R.Y*R.Y;
 
81
    if( DD < DD_min ){
 
82
      DD_min=DD;
 
83
    };
 
84
  };
 
85
  while( T->NextLineSegment() ){
 
86
    PP = position - scales.Tr(T->LineSegment.Start);
 
87
    L = scales.Tr(T->LineSegment.End) - scales.Tr(T->LineSegment.Start);
 
88
    LengthL = sqrt(L.X*L.X+L.Y*L.Y);
 
89
    PPP.X = (L.X/LengthL)*PP.X + (L.Y/LengthL)*PP.Y;
 
90
    PPP.Y = (L.Y/LengthL)*PP.X - (L.X/LengthL)*PP.Y;
 
91
    if( PPP.X < 0 ){
 
92
      DD = PPP.X*PPP.X + PPP.Y*PPP.Y;
 
93
    }else if( PPP.X > LengthL ){
 
94
      DD = (PPP.X-LengthL)*(PPP.X-LengthL) + PPP.Y*PPP.Y;
 
95
    }else{
 
96
      DD = PPP.Y*PPP.Y;
 
97
    };
 
98
    if( DD < DD_min ){
 
99
      DD_min=DD;
 
100
    };
 
101
  };
 
102
  try{
 
103
    D_min = std::min( D_min , Round(sqrt(DD_min)) );
 
104
  }catch(...){
 
105
  };
 
106
 
 
107
  if( T ){ delete T;  T=NULL; };
 
108
  return D_min;
 
109
};
 
110
 
 
111
//---------------------------------------------------------------- sFunction ---
 
112
 
 
113
sFunction::sFunction (rsObjectStream a_stream)
 
114
    :sCurve(a_stream)
 
115
{
 
116
};
 
117
 
 
118
 
 
119
void  sFunction::Store (rsObjectStream a_stream)
 
120
{
 
121
  sCurve::Store(a_stream);
 
122
};
 
123
 
 
124
 
 
125
//real  sFunction::Value (real  /*X*/)
 
126
//{
 
127
////  real                      V;
 
128
////  return V;
 
129
//  return 0.0;
 
130
//};
 
131
 
 
132
 
 
133
//-------------------------------------------------------------- sDependence ---
 
134
 
 
135
sDependence::sDependence ()
 
136
    :sFunction()
 
137
    ,sRecord()
 
138
    ,TheMultiplier()
 
139
    ,RecalculateBoundaries(true)
 
140
{
 
141
  TheMultiplier.X = sPhysValue(real_nan);
 
142
  TheMultiplier.Y = sPhysValue(real_nan);
 
143
};
 
144
 
 
145
 
 
146
sDependence::sDependence (rcsPhysPair  multiplier)
 
147
    :sFunction()
 
148
    ,sRecord()
 
149
    ,TheMultiplier(multiplier)
 
150
    ,RecalculateBoundaries(true)
 
151
{
 
152
  //this would be safe for AssignIntrinsicContent
 
153
//  TheMultiplier.X = multiplier.X;
 
154
//  TheMultiplier.Y = multiplier.Y;
 
155
};
 
156
 
 
157
 
 
158
sDependence::sDependence (rsObjectStream a_stream)
 
159
    :sFunction(a_stream)
45
160
    ,sRecord(a_stream)
 
161
    ,TheMultiplier(a_stream)
 
162
    ,RecalculateBoundaries(true)
46
163
{
47
164
};
48
165
 
49
166
 
50
 
void  sCurve::Store (rsObjectStream a_stream)
 
167
void  sDependence::Store (rsObjectStream a_stream)
51
168
{
52
 
  s2DVisibleObject::Store(a_stream);
 
169
  sFunction::Store(a_stream);
53
170
  sRecord::Store(a_stream);
 
171
  TheMultiplier.Store(a_stream);
54
172
};
55
173
 
56
174
 
57
 
sString  sCurve::Text (eTextFormat        F
58
 
                      ,eTextDetalization  D) const
 
175
sString  sDependence::Text (eTextFormat        F
 
176
                           ,eTextDetalization  D) const
59
177
{
60
178
  sString                     T;
61
 
  psCurveAppearance           A = dynamic_cast<psCurveAppearance>(Appearance);
62
 
  if( A && (F == HTML) ){
63
 
    T = "<font ";
64
 
    T += A->/*Line.*/Color.TagForHTML();
65
 
    T += ">∿</font>";
66
 
  };
67
 
  return T + sRecord::Text(F,D);
 
179
  if( F == HTML ){
 
180
    T = Symbol::CurveIcon;
 
181
    psCurveAppearance           A = dynamic_cast<psCurveAppearance>(Appearance);
 
182
    if( A ){
 
183
      T = sString("<font ") + A->/*Line.*/Color.TagForHTML() + ">"
 
184
          + T
 
185
          + "</font>";
 
186
    };
 
187
  };
 
188
  T += sRecord::Text(F,D);
 
189
  if( D > Laconic ){
 
190
    T += sString(" [") + TheMultiplier.Y.Units().Text(F)
 
191
        +sString("(") + TheMultiplier.X.Units().Text(F) + sString(")]");
 
192
  };
 
193
  return T;
68
194
};
69
195
 
70
196
 
71
 
void  sCurve::AssignIntrinsicContent (sNodePtr NP)
 
197
void  sDependence::AssignIntrinsicContent (sNodePtr NP)
72
198
{
73
199
  sRecord::AssignIntrinsicContent(NP);
74
 
//  if( NP->OwnType() <= sCurve::Type ){
75
 
//  };
76
 
};
77
 
 
78
 
 
79
 
sBoundaries  sCurve::Boundaries ()
80
 
{
81
 
  sCurve::psTracer            T = GetCurveTracer();
82
 
  sBoundaries                 B;
 
200
  sDependence::sPtr           Ptr(NP);
 
201
  if( Ptr.IsCorrect() ){
 
202
    //! @todo{Net} will throw exception, if units differ
 
203
    TheMultiplier = Ptr->TheMultiplier;
 
204
    RecalculateBoundaries=Ptr->RecalculateBoundaries;
 
205
    LastBoundaries=Ptr->LastBoundaries;
 
206
    SetEditedFlag();
 
207
  };
 
208
};
 
209
 
 
210
 
 
211
sBoundaries  sDependence::Boundaries ()
 
212
{
 
213
  if( RecalculateBoundaries ){
 
214
    LastBoundaries=CalculateBoundaries();
 
215
    RecalculateBoundaries=false;
 
216
    SetEditedFlag();
 
217
  };
 
218
  return LastBoundaries;
 
219
};
 
220
 
 
221
 
 
222
sBoundaries  sDependence::CalculateBoundaries ()
 
223
{
 
224
  sRectangle                  B;
 
225
  sCurve::psTracer            T=GetCurveTracer(sScales(sScale(Multiplier().X)
 
226
                                                      ,sScale(Multiplier().Y)));
83
227
  real                        Xmin;
84
228
  real                        Xmax;
85
229
  real                        Ymin;
86
230
  real                        Ymax;
87
231
 
 
232
  B = sRectangle( sRange(real_nan,real_nan)  , sRange(real_nan,real_nan) );
88
233
  if( T->NextPoint() ){
89
234
    Xmin = Xmax = T->Point.X;
90
235
    Ymin = Ymax = T->Point.Y;
100
245
        Ymax=T->Point.Y;
101
246
      };
102
247
    };
103
 
    B = sBoundaries( sRange(Xmin,Xmax) , sRange(Ymin,Ymax) );
 
248
    B = sRectangle( sRange(Xmin,Xmax) , sRange(Ymin,Ymax) );
104
249
  };
105
250
/*
106
251
  while( T->NextLineSegment() ){
126
271
    };
127
272
  };
128
273
*/
129
 
  if( T ){ delete T;  T=NULL; };
130
 
  return B;
131
 
};
132
 
 
133
 
 
134
 
bool  sCurve::IsUnambigousFunction () const
135
 
{
136
 
  return false;
137
 
};
138
 
 
139
 
 
140
 
real  sCurve::Value (real  /*X*/) const
141
 
{
142
 
//  real                      V;
143
 
//  return V;
144
 
  return 0.0;
145
 
};
146
 
 
147
 
int  sCurve::Distance (sPoint P
148
 
                      ,real   XPixelValue
149
 
                      ,real   YPixelValue)
150
 
{
151
 
  sCurve::psTracer            T = GetCurveTracer();
152
 
  real                        DD_min ,DD;
153
 
  real                        L ,LX ,LY;
154
 
  sPoint                      PP ,PPP;
155
 
 
156
 
  DD_min= real(std::numeric_limits<int>::max())
157
 
        * real(std::numeric_limits<int>::max());
158
 
  while( T->NextPoint() ){
159
 
    DD=((P.X - T->Point.X)/XPixelValue)*((P.X - T->Point.X)/XPixelValue)
160
 
      +((P.Y - T->Point.Y)/YPixelValue)*((P.Y - T->Point.Y)/YPixelValue);
161
 
    if( DD < DD_min ){
162
 
      DD_min=DD;
163
 
    };
164
 
  };
165
 
  while( T->NextLineSegment() ){
166
 
    PP = P;
167
 
    PP.X = (PP.X - T->LineSegment.Start.X) / XPixelValue;
168
 
    PP.Y = (PP.Y - T->LineSegment.Start.Y) / YPixelValue;
169
 
    LX = (T->LineSegment.End.X - T->LineSegment.Start.X) / XPixelValue;
170
 
    LY = (T->LineSegment.End.Y - T->LineSegment.Start.Y) / YPixelValue;
171
 
    L = sqrt(LX*LX+LY*LY);
172
 
    PPP.X = (LX/L)*PP.X + (LY/L)*PP.Y;
173
 
    PPP.Y = (LY/L)*PP.X - (LX/L)*PP.Y;
174
 
    if( PPP.X < 0 ){
175
 
      DD = PPP.X*PPP.X + PPP.Y*PPP.Y;
176
 
    }else if( PPP.X > L ){
177
 
      DD = (PPP.X-L)*(PPP.X-L) + PPP.Y*PPP.Y;
178
 
    }else{
179
 
      DD = PPP.Y*PPP.Y;
180
 
    };
181
 
    if( DD < DD_min ){
182
 
      DD_min=DD;
183
 
    };
184
 
  };
185
 
  if( T ){ delete T;  T=NULL; };
186
 
  try{
187
 
    return Round(sqrt(DD_min));
188
 
  }catch(...){
189
 
    return std::numeric_limits<int>::max();
190
 
  };
 
274
 
 
275
//  RANet::Log.Put(sLog::Debug,"Graph"
 
276
//      ,sString("CalculateBoundaries: ")
 
277
//      +" MX="+Multiplier().X.Text(Plain,Verbose)
 
278
//      +" MY="+Multiplier().Y.Text(Plain,Verbose)
 
279
//      +(sString(" X.From=") << B.X.From)
 
280
//      +(sString(" X.To=") << B.X.To)
 
281
//      +(sString(" Y.From=") << B.Y.From)
 
282
//      +(sString(" Y.To=") << B.Y.To)
 
283
//      );
 
284
 
 
285
  if( T ){ delete T;  T=NULL; };
 
286
  return sBoundaries(sPhysRange(B.X.From*Multiplier().X.ArbValue()
 
287
                               ,B.X.To*Multiplier().X.ArbValue()
 
288
                               ,Multiplier().X.Units()          )
 
289
                    ,sPhysRange(B.Y.From*Multiplier().Y.ArbValue()
 
290
                               ,B.Y.To*Multiplier().Y.ArbValue()
 
291
                               ,Multiplier().Y.Units()          )
 
292
                    );
191
293
};
192
294
 
193
295
//------------------------------------------------------------------------------