~d-nelson/research-assistant/nextDNK

« back to all changes in this revision

Viewing changes to RANet/Curves.cpp

  • Committer: Viktor Bursian
  • Date: 2013-06-06 13:42:39 UTC
  • Revision ID: vbursian@gmail.com-20130606134239-bx5ks8sg3y9oqckz
Tags: version_0.2.0
first usable version 0.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
////////////////////////////////////////////////////////////////////////////////
 
2
/*! @file Curves.cpp   Абстракция кривых и функций.
 
3
- Part of RANet - Research Assistant Net Library (based on ANSI C++).
 
4
- Copyright(C) 2000-2010, Viktor E. Bursian, St.Petersburg, Russia.
 
5
                     Viktor.Bursian@mail.ioffe.ru
 
6
*///////////////////////////////////////////////////////////////////////////////
 
7
#include "Curves.h"
 
8
#include "Excepts.h"
 
9
#include <math.h>
 
10
#include <limits>
 
11
namespace RA {
 
12
//------------------------------------------------------------------------------
 
13
 
 
14
DEFINE_CLASS_TAG(sCurveAppearance)
 
15
DEFINE_CLASS_TAG(sCurve)
 
16
 
 
17
//--------------------------------------------------------- sCurveAppearance ---
 
18
 
 
19
sCurveAppearance::sCurveAppearance ()
 
20
{
 
21
  Color=sColor::cRed;
 
22
};
 
23
 
 
24
 
 
25
sCurveAppearance::sCurveAppearance (rsObjectStream S)
 
26
    :sAppearance(S)
 
27
    ,Points(S)
 
28
    ,Line(S)
 
29
{
 
30
};
 
31
 
 
32
 
 
33
void  sCurveAppearance::Store (rsObjectStream S)
 
34
{
 
35
  sAppearance::Store(S);
 
36
  Points.Store(S);
 
37
  Line.Store(S);
 
38
};
 
39
 
 
40
 
 
41
//------------------------------------------------------------------- sCurve ---
 
42
 
 
43
sCurve::sCurve (rsObjectStream a_stream)
 
44
    :s2DVisibleObject(a_stream)
 
45
    ,sRecord(a_stream)
 
46
{
 
47
};
 
48
 
 
49
 
 
50
void  sCurve::Store (rsObjectStream a_stream)
 
51
{
 
52
  s2DVisibleObject::Store(a_stream);
 
53
  sRecord::Store(a_stream);
 
54
};
 
55
 
 
56
 
 
57
sString  sCurve::Text (eTextFormat        F
 
58
                      ,eTextDetalization  D) const
 
59
{
 
60
  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);
 
68
};
 
69
 
 
70
 
 
71
void  sCurve::AssignIntrinsicContent (sNodePtr NP)
 
72
{
 
73
  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;
 
83
  real                        Xmin;
 
84
  real                        Xmax;
 
85
  real                        Ymin;
 
86
  real                        Ymax;
 
87
 
 
88
  if( T->NextPoint() ){
 
89
    Xmin = Xmax = T->Point.X;
 
90
    Ymin = Ymax = T->Point.Y;
 
91
    while( T->NextPoint() ){
 
92
      if( T->Point.X < Xmin ){
 
93
        Xmin=T->Point.X;
 
94
      }else if( Xmax < T->Point.X ){
 
95
        Xmax=T->Point.X;
 
96
      };
 
97
      if( T->Point.Y < Ymin ){
 
98
        Ymin=T->Point.Y;
 
99
      }else if( Ymax < T->Point.Y ){
 
100
        Ymax=T->Point.Y;
 
101
      };
 
102
    };
 
103
    B = sBoundaries( sRange(Xmin,Xmax) , sRange(Ymin,Ymax) );
 
104
  };
 
105
/*
 
106
  while( T->NextLineSegment() ){
 
107
    if( T->LineSegment.Start.X < B.X.From ){
 
108
      B.X.From=T->LineSegment.Start.X;
 
109
    }else if( T->LineSegment.Start.X > B.X.To ){
 
110
      B.X.To=T->LineSegment.Start.X;
 
111
    };
 
112
    if( T->LineSegment.Start.Y < B.Y.From ){
 
113
      B.Y.From=T->LineSegment.Start.Y;
 
114
    }else if( T->LineSegment.Start.Y > B.Y.To ){
 
115
      B.Y.To=T->LineSegment.Start.Y;
 
116
    };
 
117
    if( T->LineSegment.End.X < B.X.From ){
 
118
      B.X.From=T->LineSegment.End.X;
 
119
    }else if( T->LineSegment.End.X > B.X.To ){
 
120
      B.X.To=T->LineSegment.End.X;
 
121
    };
 
122
    if( T->LineSegment.End.Y < B.Y.From ){
 
123
      B.Y.From=T->LineSegment.End.Y;
 
124
    }else if( T->LineSegment.End.Y > B.Y.To ){
 
125
      B.Y.To=T->LineSegment.End.Y;
 
126
    };
 
127
  };
 
128
*/
 
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
  };
 
191
};
 
192
 
 
193
//------------------------------------------------------------------------------
 
194
}; //namespace RA
 
195
 
 
196
 
 
197
 
 
198