summaryrefslogtreecommitdiffstats
path: root/ksvg/plugin/backends/agg/BezierPathAgg.cpp
blob: c956f92b46324480e8db5c0ec6c8e51ceccdb5da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
    Copyright (C) 2003 Nikolas Zimmermann <wildfox@kde.org>
    This file is part of the KDE project

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    aint with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include <math.h>
#include "Point.h"
#include "BezierPathAgg.h"

#include <kdebug.h>

#include <agg_basics.h>
#include <agg_bounding_rect.h>

using namespace T2P;

double BezierPathAgg::length(double t)
{
	if(m_length < 0.0)
	{
		double total = 0.0;
		double x = 0.0, y = 0.0;
		double x2, y2;
		unsigned cmd;
		unsigned int id = 0;
		m_curved_trans.rewind(id);
		while(!agg::is_stop(cmd = m_curved_trans.vertex(&x2, &y2)))
		{
			if(agg::is_move_to(cmd))
			{
				x = x2;
				y = y2;	
			}
			else if(agg::is_line_to(cmd))
			{
				double dx = x, dy = y;
				dx = x2 - dx;
				dy = y2 - dy;
				total += sqrt(pow(dx, 2) + pow(dy, 2));
				x = x2;
				y = y2;
			}
		}
		return total * t;
	}
	else
		return m_length * t;
}

void BezierPathAgg::pointTangentNormalAt(double t, Point *p, Point *tn, Point *n)
{
	double totallen = length(t);
	double total = 0.0;
        double x = 0.0, y = 0.0;
	double x2, y2;
	unsigned cmd;
	unsigned int id = 0;
	m_curved_trans.rewind(id);
	while(!agg::is_stop(cmd = m_curved_trans.vertex(&x2, &y2)))
	{
		if(agg::is_move_to(cmd))
		{
			x = x2;
			y = y2;	
		}
		else if(agg::is_line_to(cmd))
		{
			double dx = x, dy = y;
			x = x2;
			y = y2;
			dx = x - dx;
			dy = y - dy;
			double seg_len = sqrt(pow(dx, 2) + pow(dy, 2));
			total += seg_len;
			if(total >= totallen)
			{
				double fract = 1 - (totallen - (total - seg_len)) / seg_len;
				if(p)
				{
					p->setX(x - dx * fract);
					p->setY(y - dy * fract);
				}
				if(tn)
				{
					//kdDebug() << k_funcinfo << "dx : " << dx << endl;
					//kdDebug() << k_funcinfo << "dy : " << dy << endl;
					tn->setX(dx);
					tn->setY(dy);
				}
				if(n)
				{
					// Calculate vector product of "binormal" x tangent
					// (0,0,1) x (dx,dy,0), which is simply (dy,-dx,0).
					n->setX(dy);
					n->setY(-dx);
				}
				return;
			}
		}
	}
}

void BezierPathAgg::boundingBox(Point *topLeft, Point *bottomRight)
{
	double x1, y1, x2, y2;
	agg::bounding_rect(m_curved, *this, 0, 1, &x1, &y1, &x2, &y2);
	*topLeft = Point(x1, y1);
	*bottomRight = Point(x2, y2);
}