#include "language.h"
#include "vframe.h"
-void SketcherPoint::init(int id, int x, int y)
+void SketcherPoint::init(int id, int pty, int x, int y)
{
- this->id = id;
+ this->id = id; this->pty = pty;
this->x = x; this->y = y;
}
SketcherPoint::SketcherPoint(int id)
{
- init(id, 0, 0);
+ init(id, PTY_LINE, 0, 0);
}
-SketcherPoint::SketcherPoint(int id, int x, int y)
+SketcherPoint::SketcherPoint(int id, int pty, int x, int y)
{
- init(id, x, y);
+ init(id, pty, x, y);
}
SketcherPoint::~SketcherPoint()
{
int SketcherPoint::equivalent(SketcherPoint &that)
{
return this->id == that.id &&
+ this->pty == that.pty &&
this->x == that.x &&
this->y == that.y ? 1 : 0;
}
void SketcherPoint::copy_from(SketcherPoint &that)
{
- this->id = that.id;
+ this->id = that.id; this->pty = that.pty;
this->x = that.x; this->y = that.y;
}
void SketcherPoint::save_data(FileXML &output)
char point[BCSTRLEN];
sprintf(point,"/POINT_%d",id);
output.tag.set_title(point+1);
+ output.tag.set_property("TYPE", pty);
output.tag.set_property("X", x);
output.tag.set_property("Y", y);
output.append_tag();
void SketcherPoint::read_data(FileXML &input)
{
id = atoi(input.tag.get_title() + 6);
+ pty = input.tag.get_property("TYPE", PTY_OFF);
x = input.tag.get_property("X", 0.f);
y = input.tag.get_property("Y", 0.f);
+ bclamp(pty, 0, PTY_SZ-1);
}
-void SketcherCurve::init(int id, int ty, int radius, int pen, int color)
+void SketcherCurve::init(int id, int pen, int radius, int color)
{
this->id = id;
- this->ty = ty;
this->radius = radius;
this->pen = pen;
this->color = color;
}
SketcherCurve::SketcherCurve(int id)
{
- init(id, 0, 1, 0, BLACK);
+ init(id, 1, PTY_LINE, CV_COLOR);
}
-SketcherCurve::SketcherCurve(int id, int ty, int radius,int pen, int color)
+SketcherCurve::SketcherCurve(int id, int pen, int radius, int color)
{
- init(id, ty, radius, pen, color);
+ init(id, pen, radius, color);
}
SketcherCurve::~SketcherCurve()
{
int SketcherCurve::equivalent(SketcherCurve &that)
{
if( this->id != that.id ) return 0;
- if( this->ty != that.ty ) return 0;
- if( this->radius != that.radius ) return 0;
if( this->pen != that.pen ) return 0;
+ if( this->radius != that.radius ) return 0;
if( this->color != that.color ) return 0;
int n = this->points.size();
if( n != that.points.size() ) return 0;
void SketcherCurve::copy_from(SketcherCurve &that)
{
this->id = that.id;
- this->ty = that.ty;
- this->radius = that.radius;
this->pen = that.pen;
+ this->radius = that.radius;
this->color = that.color;
int m = points.size(), n = that.points.size();
while( m > n ) points.remove_object_number(--m);
}
void SketcherCurve::save_data(FileXML &output)
{
- this->ty = ty;
this->pen = pen; this->color = color;
char curve[BCSTRLEN];
sprintf(curve,"/CURVE_%d",id);
output.tag.set_title(curve+1);
- output.tag.set_property("TYPE", ty);
- output.tag.set_property("RADIUS", radius);
output.tag.set_property("PEN", pen);
+ output.tag.set_property("RADIUS", radius);
output.tag.set_property("COLOR", color);
output.append_tag();
output.append_newline();
void SketcherCurve::read_data(FileXML &input)
{
id = atoi(input.tag.get_title() + 6);
- ty = input.tag.get_property("TYPE", 0);
+ pen = input.tag.get_property("PEN", PTY_OFF);
radius = input.tag.get_property("RADIUS", 1.);
- pen = input.tag.get_property("PEN", 0);
- color = input.tag.get_property("COLOR", BLACK);
+ color = input.tag.get_property("COLOR", CV_COLOR);
+ bclamp(pen, 0, PEN_SZ-1);
}
-int Sketcher::new_curve(int ty, int radius, int pen, int color)
+int Sketcher::new_curve(int pen, int radius, int color)
{
SketcherCurves &curves = config.curves;
int k = curves.size(), id = 1;
int n = config.curves[i]->id;
if( n >= id ) id = n + 1;
}
- SketcherCurve *cv = new SketcherCurve(id, ty, radius, pen, color);
+ SketcherCurve *cv = new SketcherCurve(id, pen, radius, color);
curves.append(cv);
config.cv_selected = k;
return k;
int Sketcher::new_curve()
{
- return new_curve(0, 1, 0, BLACK);
+ return new_curve(PEN_XLANT, 1, CV_COLOR);
}
-int Sketcher::new_point(SketcherCurve *cv, int x, int y)
+int Sketcher::new_point(SketcherCurve *cv, int pty, int x, int y, int idx)
{
- int k = cv->points.size(), id = 1;
- for( int i=k; --i>=0; ) {
+ int id = 1;
+ for( int i=cv->points.size(); --i>=0; ) {
int n = cv->points[i]->id;
if( n >= id ) id = n + 1;
}
- SketcherPoint *pt = new SketcherPoint(id, x, y);
- cv->points.append(pt);
- return k;
+ SketcherPoint *pt = new SketcherPoint(id, pty, x, y);
+ int n = cv->points.size();
+ if( idx < 0 || idx > n ) idx = n;
+ cv->points.insert(pt, idx);
+ return idx;
}
-int Sketcher::new_point()
+int Sketcher::new_point(int idx)
{
int ci = config.cv_selected;
if( ci < 0 || ci >= config.curves.size() )
EDLSession *session = get_edlsession();
int x = !session ? 0.f : session->output_w / 2.f;
int y = !session ? 0.f : session->output_h / 2.f;
- return new_point(cv, x, y);
+ return new_point(cv, PTY_LINE, x, y, idx);
}
REGISTER_PLUGIN(Sketcher)
while( --k >= 0 && pcv->id != (ncv=next.curves[k])->id );
if( k >= 0 ) {
cv->id = pcv->id;
- cv->ty = pcv->ty;
- cv->radius = pcv->radius;
cv->pen = pcv->pen;
+ cv->radius = pcv->radius;
cv->color = pcv->color;
int prev_pt_sz = pcv->points.size(), next_pt_sz = ncv->points.size();
for( int j=0; j<prev_pt_sz; ++j ) {
x = x * prev_scale + nt->x * next_scale;
y = y * prev_scale + nt->y * next_scale;
}
- cv->points.append(new SketcherPoint(pt.id, x, y));
+ cv->points.append(new SketcherPoint(pt.id, pt.pty, x, y));
}
}
else
output.tag.set_title("SKETCHER");
output.tag.set_property("DRAG", config.drag);
- output.tag.set_property("CURVE_SELECTED", config.cv_selected);
- output.tag.set_property("POINT_SELECTED", config.pt_selected);
+ output.tag.set_property("CV_SELECTED", config.cv_selected);
+ output.tag.set_property("PT_SELECTED", config.pt_selected);
output.append_tag();
output.append_newline();
for( int i=0,n=config.curves.size(); i<n; ++i ) {
}
}
- if( !config.curves.size() ) {
- new_curve(0, 1, 0, BLACK);
- }
+ if( !config.curves.size() )
+ new_curve();
config.limits();
}
void Sketcher::draw_point(VFrame *vfrm, SketcherPoint *pt, int color, int d)
{
int r = d/2+1, x = pt->x, y = pt->y;
+ vfrm->set_pixel_color(color);
vfrm->draw_smooth(x-r,y+0, x-r, y-r, x+0,y-r);
vfrm->draw_smooth(x+0,y-r, x+r, y-r, x+r,y+0);
vfrm->draw_smooth(x+r,y+0, x+r, y+r, x+0,y+r);
vfrm->draw_smooth(x+0,y+r, x-r, y+r, x-r,y+0);
- vfrm->set_pixel_color(color);
vfrm->draw_x(pt->x, pt->y, d);
}
void Sketcher::draw_point(VFrame *vfrm, SketcherPoint *pt, int color)
return 0;
}
-void SketcherCurve::draw_line(VFrame *out)
+static int intersects_at(float &x, float &y,
+ float ax,float ay, float bx, float by, float cx,float cy, // line slope ab thru c
+ float dx,float dy, float ex, float ey, float fx,float fy, // line slope de thru f
+ float mx=0)
{
- SketcherPoint *pt0 = points[0];
- VFrame *vpen = new_vpen(out);
- out->set_pixel_color(color);
- int n = points.size();
- if( n >= 2 ) {
- for( int pi=1; pi<n; ++pi ) {
- SketcherPoint *pt1 = points[pi];
- vpen->draw_line(pt0->x, pt0->y, pt1->x, pt1->y);
- pt0 = pt1;
- }
- }
- else
- vpen->draw_pixel(pt0->x, pt0->y);
- delete vpen;
+ float badx = bx - ax, bady = by - ay;
+ float eddx = ex - dx, eddy = ey - dy;
+ float d = badx*eddy - bady*eddx;
+ int ret = 0;
+ if( fabsf(d) < 1 ) { ret = 1; d = signbit(d) ? -1 : 1; }
+ x = (badx*cy*eddx - badx*eddx*fy + badx*eddy*fx - bady*cx*eddx) / d;
+ y = (badx*cy*eddy - bady*cx*eddy - bady*eddx*fy + bady*eddy*fx) / d;
+ if( mx > 0 ) { bclamp(x, -mx,mx); bclamp(y, -mx,mx); }
+ return ret;
}
-/*
-# python
-from sympy import *
-var("x,y, ax,ay, bx,by, cx,cy, dx,dy")
-
-var("abdx,abdy, acdx,acdy, bddx,bddy, cddx,cddy");
-abdx = bx-ax; abdy = by-ay;
-acdx = cx-ax; acdy = cy-ay;
-bddx = dx-bx; bddy = dy-by;
-cddx = dx-cx; cddy = dy-cy;
-
-var("xc,yc, xd,yd, sx,sy");
-xc = (bx+dx)/2; yc = (by+dy)/2;
-xd = cx-xc; yd = cy-yc;
-ax = xc-xd; ay = yc-yd;
-# line thru b with slope (c-a) intersects line thru c with slope (d-b)
-sx = solve(((x - bx) * acdy/acdx + by) - ((x - cx) * bddy/bddx + cy),x)
-sy = solve(((y - by) * acdx/acdy + bx) - ((y - cy) * bddx/bddy + cx),y)
-
-var("zx,zy, zdx,zdy, sx,sy, px,py, qx,qy");
-# point z = (b+c)/2
-zx = (bx+cx)/2; zy = (by+cy)/2;
-zdx = (abdx+cddx)/2; zdy = (abdy+cddy)/2;
-# line thru z with slope (d-a) intersects line thru b with slope (c-a)
-px = solve(((x-zx)*zdy/zdx + zy) - ((x-bx) * acdy/acdx + by),x);
-py = solve(((y-zy)*zdx/zdy + zx) - ((y-by) * acdx/acdy + bx),y);
-# line thru z with slope (c-a + d-b)/2 intersects line thru c with slope (d-b)
-qx = solve(((x-zx)*zdy/zdx + zy) - ((x-cx) * bddy/bddx + cy),x);
-qy = solve(((y-zy)*zdx/zdy + zx) - ((y-cy) * bddx/bddy + cx),y);
-*/
-
-static void smooth_sxy(
- float ax, float ay, float bx, float by,
- float cx, float cy, float dx, float dy,
- float &sx, float &sy)
+static void smooth_axy(float &ax, float &ay,
+ float bx, float by, float cx, float cy, float dx, float dy)
{
- float acdx = cx-ax, acdy = cy-ay;
- float bddx = dx-bx, bddy = dy-by;
- float d = acdx*bddy - acdy*bddx;
- if( fabsf(d) < 1 ) d = 1;
- sx = (acdx*bddx*by - acdx*bddx*cy + acdx*bddy*cx - acdy*bddx*bx) / d;
- sy = (acdx*bddy*by - acdy*bddx*cy - acdy*bddy*bx + acdy*bddy*cx) / d;
- bclamp(sx, -4095.f, 4095.f);
- bclamp(sy, -4095.f, 4095.f);
-}
-
-static void smooth_pxy(
- float ax, float ay, float bx, float by,
- float cx, float cy, float dx, float dy,
- float &px, float &py)
-{
- float abdx = bx - ax, abdy = by - ay;
- float acdx = cx - ax, acdy = cy - ay;
- float cddx = dx - cx, cddy = dy - cy;
- float d = (2*(abdx*acdy - abdy*acdx - acdx*cddy + acdy*cddx));
- if( fabsf(d) < 1 ) d = 1;
- px = (-abdx*acdx*by + abdx*acdx*cy + 2*abdx*acdy*bx - abdy*acdx*bx - abdy*acdx*cx -
- acdx*bx*cddy - acdx*by*cddx + acdx*cddx*cy - acdx*cddy*cx + 2*acdy*bx*cddx) / d;
- py = (abdx*acdy*by + abdx*acdy*cy - 2*abdy*acdx*by + abdy*acdy*bx - abdy*acdy*cx -
- 2*acdx*by*cddy + acdy*bx*cddy + acdy*by*cddx + acdy*cddx*cy - acdy*cddy*cx) / d;
- bclamp(px, -4095.f, 4095.f);
- bclamp(py, -4095.f, 4095.f);
-}
-static void smooth_qxy(
- float ax, float ay, float bx, float by,
- float cx, float cy, float dx, float dy,
- float &qx, float &qy)
-{
- float abdx = bx - ax, abdy = by - ay;
- float bddx = dx - bx, bddy = dy - by;
- float cddx = dx - cx, cddy = dy - cy;
- float d = (2*(abdx*bddy - abdy*bddx - bddx*cddy + bddy*cddx));
- if( fabsf(d) < 1 ) d = 1;
- qx = (abdx*bddx*by - abdx*bddx*cy + 2*abdx*bddy*cx - abdy*bddx*bx - abdy*bddx*cx -
- bddx*bx*cddy + bddx*by*cddx - bddx*cddx*cy - bddx*cddy*cx + 2*bddy*cddx*cx) / d;
- qy = (abdx*bddy*by + abdx*bddy*cy - 2*abdy*bddx*cy - abdy*bddy*bx + abdy*bddy*cx -
- 2*bddx*cddy*cy - bddy*bx*cddy + bddy*by*cddx + bddy*cddx*cy + bddy*cddy*cx) / d;
- bclamp(qx, -4095.f, 4095.f);
- bclamp(qy, -4095.f, 4095.f);
+//middle of bd reflected around ctr
+// point ctr = b+d/2, dv=c-ctr, a=ctr-dv;
+ float xc = (bx+dx)*.5f, yc = (by+dy)*.5f;
+ float xd = cx - xc, yd = cy - yc;
+ ax = xc - xd; ay = yc - yd;
+}
+static void smooth_dxy(float &dx, float &dy,
+ float ax, float ay, float bx, float by, float cx, float cy)
+{
+//middle of ac reflected around ctr
+// point ctr = a+c/2, dv=c-ctr, d=ctr-dv;
+ float xc = (ax+cx)*.5f, yc = (ay+cy)*.5f;
+ float xd = bx - xc, yd = by - yc;
+ dx = xc - xd; dy = yc - yd;
}
-
+#if 0
static int convex(float ax,float ay, float bx,float by,
float cx,float cy, float dx,float dy)
{
- float abdx = bx - ax, abdy = by - ay;
- float acdx = cx - ax, acdy = cy - ay;
- float bcdx = cx - bx, bcdy = cy - by;
- float bddx = dx - bx, bddy = dy - by;
+ float abdx = bx-ax, abdy = by-ay;
+ float acdx = cx-ax, acdy = cy-ay;
+ float bcdx = cx-bx, bcdy = cy-by;
+ float bddx = dx-bx, bddy = dy-by;
float abc = abdx*acdy - abdy*acdx;
float bcd = bcdx*bddy - bcdy*bddx;
float v = abc * bcd;
return !v ? 0 : v>0 ? 1 : -1;
}
+#endif
-void SketcherCurve::draw_smooth(VFrame *out)
+void SketcherCurve::draw(VFrame *out)
{
+ const float fmx = 16383;
VFrame *vpen = new_vpen(out);
out->set_pixel_color(color);
int n = points.size();
if( !n ) return;
if( n > 2 ) {
- SketcherPoint *pt0 = points[0], *pt1 = points[1], *pt2 = points[2];
- float bx = pt0->x, by = pt0->y;
- float cx = pt1->x, cy = pt1->y;
- float dx = pt2->x, dy = pt2->y;
- float xc = (bx+dx)/2.f, yc = (by+dy)/2.f;
- float xd = cx - xc, yd = cy - yc;
- float ax = xc - xd, ay = yc - yd;
- float sx, sy;
- for( int pi=0,n2=n-2; pi<n2; ++pi ) {
- float dx = points[pi+2]->x, dy = points[pi+2]->y;
- if( convex(ax,ay, bx,by, cx,cy, dx,dy) >= 0 ) {
- smooth_sxy(ax,ay, bx,by, cx,cy, dx,dy, sx, sy);
+ int n2 = n - 2;
+ SketcherPoint *pt0 = points[0];
+ SketcherPoint *pt1 = points[1];
+ SketcherPoint *pt2 = points[2];
+ float ax,ay, bx,by, cx,cy, dx,dy, sx,sy;
+ bx = pt0->x; by = pt0->y;
+ cx = pt1->x; cy = pt1->y;
+ dx = pt2->x; dy = pt2->y;
+ smooth_axy(ax,ay, bx,by, cx,cy, dx,dy);
+ for( int pi=0; pi<n2; ++pi ) {
+ int pty = points[pi]->pty;
+ dx = points[pi+2]->x; dy = points[pi+2]->y;
+ switch( pty ) {
+ case PTY_LINE:
+ vpen->draw_line(bx, by, cx, cy);
+ break;
+ case PTY_CURVE: {
+ // s = ac thru b x bd thru c
+ intersects_at(sx,sy, ax,ay,cx,cy,bx,by, bx,by,dx,dy,cx,cy,fmx);
vpen->draw_smooth(bx,by, sx,sy, cx,cy);
- }
- else {
- float zx = (bx+cx)/2.f, zy = (by+cy)/2.f;
- smooth_pxy(ax,ay, bx,by, cx,cy, dx,dy, sx,sy);
- vpen->draw_smooth(bx,by, sx,sy, zx,zy);
- smooth_qxy(ax,ay, bx,by, cx,cy, dx,dy, sx,sy);
- vpen->draw_smooth(zx,zy, sx,sy, cx,cy);
+ break; }
}
ax = bx; ay = by;
bx = cx; by = cy;
cx = dx; cy = dy;
}
- xc = (ax+cx)/2.f; yc = (ay+cy)/2.f;
- xd = bx - xc, yd = by - yc;
- dx = xc - xd, dy = yc - yd;
- smooth_sxy(ax, ay, bx, by, cx, cy, dx, dy, sx, sy);
- vpen->draw_smooth(bx, by, sx, sy, cx, cy);
+ switch( points[n2]->pty ) {
+ case PTY_LINE:
+ vpen->draw_line(bx, by, cx, cy);
+ break;
+ case PTY_CURVE: {
+ smooth_dxy(dx,dy, ax,ay, bx,by, cx,cy);
+ intersects_at(sx,sy, ax,ay,cx,cy,bx,by, bx,by,dx,dy,cx,cy,fmx);
+ vpen->draw_smooth(bx,by, sx,sy, cx,cy);
+ break; }
+ }
}
else if( n == 2 ) {
SketcherPoint *pt0 = points[0], *pt1 = points[1];
for( int ci=0, n=config.curves.size(); ci<n; ++ci ) {
SketcherCurve *cv = config.curves[ci];
int m = cv->points.size();
- if( !m ) continue;
- switch( cv->ty ) {
- case TYP_OFF:
- break;
- case TYP_SKETCHER:
- cv->draw_line(output);
- break;
- case TYP_SMOOTH:
- cv->draw_smooth(output);
- break;
- }
+ if( !m || cv->pen == PTY_OFF ) continue;
+ cv->draw(output);
}
if( config.drag ) {
for( int ci=0, n=config.curves.size(); ci<n; ++ci ) {
SketcherCurve *cv = config.curves[ci];
- for( int pi=0,m=cv->points.size(); pi<m; ++pi )
- draw_point(output, cv->points[pi], cv->color);
+ for( int pi=0,m=cv->points.size(); pi<m; ++pi ) {
+ int color = ci==config.cv_selected && pi==config.pt_selected ?
+ RED : cv->color ;
+ draw_point(output, cv->points[pi], color);
+ }
}
}
{
for( int i=0; i<size(); ++i ) {
SketcherCurve *cv = get(i);
- printf("Curve %d, id=%d, ty=%s, r=%d, pen=%s, color=%02x%02x%02x\n",
- i, cv->id, cv_type[cv->ty], cv->radius, cv_pen[cv->pen],
+ printf("Curve %d, id=%d, pen=%s, r=%d, color=%02x%02x%02x\n",
+ i, cv->id, cv_pen[cv->pen], cv->radius,
(cv->color>>16)&0xff, (cv->color>>8)&0xff, (cv->color>>0)&0xff);
cv->points.dump();
}
{
for( int i=0; i<size(); ++i ) {
SketcherPoint *pt = get(i);
- printf(" Pt %d, id=%d, x=%d, y=%d\n", i, pt->id, pt->x, pt->y);
+ printf(" Pt %d, id=%d, pty=%s, x=%d, y=%d\n",
+ i, pt->id, pt_type[pt->pty], pt->x, pt->y);
}
}