Skip to content
OE_pointcurve.cpp 3.59 KiB
Newer Older
3dsman's avatar
3dsman committed
* This file is part of project OpenEmbroidery. It's copyrighted by
* the contributors recorded in the version control history of the file.
* Original project location https://code.electrolab.fr/openEmbroidery/openEmbroidery_software
*
* SPDX-License-Identifier: CECILL-2.1
* License-Filename: Licence_CeCILL_V2.1-en.txt
*/
#include "curves/OE_pointcurve.h"
#include "OE_utils.h"
#include <iostream>

#include <GL/gl.h>
#include <cstdlib>
#include <math.h>
#include <cstdio>
#include <cstring>
#include <algorithm>

raoul's avatar
raoul committed
unsigned char OE_pointcurve::controlLineColor[] = {100, 60, 92, 255};
unsigned char OE_pointcurve::controlPointColor[] = {100, 60, 92, 255};
unsigned char OE_pointcurve::controlEndPointColor[] = {100, 60, 92, 255};
raoul's avatar
raoul committed
OE_pointcurve::OE_pointcurve()
{
}

OE_pointcurve::OE_pointcurve(std::vector<vector_2d> points, bool closed)
{
raoul's avatar
raoul committed
	pts = points;
	this->closed = closed; // Flag indicating if shapes should be treated as closed.
}

OE_pointcurve::~OE_pointcurve()
{
}

bool OE_pointcurve::getPoint(uint16_t nb, float* x, float* y)
{
raoul's avatar
raoul committed
	if (pts.size() && nb<=pts.size())
	{
		*x = pts.at(nb-1).x;
		*y = pts.at(nb-1).y;
		return true;
bool OE_pointcurve::getPoint(uint16_t nb, vector_2d& pt)
raoul's avatar
raoul committed
	if (pts.size() && nb<=pts.size())
	{
		pt.x = pts.at(nb-1).x;
		pt.y = pts.at(nb-1).y;
		return true;
	}
	return false;
}

bool OE_pointcurve::setPoint(uint16_t nb, float x, float y)
{
raoul's avatar
raoul committed
	return setPoint(nb, vector_2d(x, y));
bool OE_pointcurve::setPoint(uint16_t nb, vector_2d coord)
{
raoul's avatar
raoul committed
		setNeedRefresh();
		return true;
	}
	return false;
bool OE_pointcurve::movePoint(uint16_t nb, vector_2d coord)
{
	nb--;
	if (nb<pts.size())
	{
		vector_2d offset = coord - pts[(nb)];
raoul's avatar
raoul committed
		if (!(nb%3))
raoul's avatar
raoul committed
			if (closed && (nb==0 || nb==(pts.size()-1)))
			{
				pts[0] = pts[0] + offset;
				pts[1] = pts[1] + offset;
				pts[pts.size()-2] = pts[pts.size()-2] + offset;
				pts[pts.size()-1] = pts[pts.size()-1] + offset;
raoul's avatar
raoul committed
			}
			else
raoul's avatar
raoul committed
				if (nb>0)
				{
					pts[nb-1] = pts[nb-1] + offset;
				}
				if (nb != pts.size()-1)
				{
					pts[nb+1] = pts[nb+1] + offset;
				}
		}
		pts[(nb)] = coord;
		setNeedRefresh();
		return true;
	}
	return false;
}

raoul's avatar
raoul committed
bool OE_pointcurve::addPoint(float x, float y)
raoul's avatar
raoul committed
	pts.push_back(vector_2d(x, y));
raoul's avatar
raoul committed
bool OE_pointcurve::addPoint(vector_2d pt)
{
	pts.push_back(pt);
	return true;
}

void OE_pointcurve::lineTo(float x, float y)
{
	float px,py, dx,dy;
raoul's avatar
raoul committed
	if (pts.size() > 0)
	{
		getPoint(pts.size(), &px, &py);
		dx = x - px;
		dy = y - py;
		addPoint(px + dx/3.0f, py + dy/3.0f);
		addPoint(x - dx/3.0f, y - dy/3.0f);
		addPoint(x, y);
	}
}

void OE_pointcurve::lineTo(vector_2d pt)
{
	vector_2d p,d;
raoul's avatar
raoul committed
	if (pts.size()>0)
	{
		getPoint(pts.size(), p);
raoul's avatar
raoul committed

		addPoint(p + d/3.0f);
		addPoint(pt - d/3.0f);
		addPoint(pt);
	}
}

void OE_pointcurve::setClosed(bool closed)
{
	if (pts.size() > 0)
raoul's avatar
raoul committed
	{
		if (closed && pts[0]!=pts[pts.size()-1])
		{
raoul's avatar
raoul committed
		}
	}
void OE_pointcurve::cubicBezTo(float cpx1, float cpy1, float cpx2, float cpy2, float x, float y)
{
	addPoint(cpx1, cpy1);
	addPoint(cpx2, cpy2);
	addPoint(x, y);
}
void OE_pointcurve::refresh(float dpi, bool force)
	OE_curve::refresh(dpi, force);
void OE_pointcurve::move(vector_2d offset)
{
raoul's avatar
raoul committed
	for (unsigned i=0; i<pts.size(); i++)
	{
		pts[i] = pts[i] + offset;
	}
	setNeedRefresh();
}

void OE_pointcurve::scale(vector_2d ratio, vector_2d pos)
{
	//vector_2d bbcenter = bounds.getCenter();
raoul's avatar
raoul committed
	for (unsigned i=0; i<pts.size(); i++)
	{
		pts[i] = pos + (pts[i] - pos)*ratio;
	}
	setNeedRefresh();
}