summaryrefslogtreecommitdiff
path: root/dviware/dvisvgm/src/TriangularPatch.cpp
blob: c57836d900239ceb63dc69b1576471cdb6a87922 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*************************************************************************
** TriangularPatch.cpp                                                  **
**                                                                      **
** This file is part of dvisvgm -- a fast DVI to SVG converter          **
** Copyright (C) 2005-2021 Martin Gieseking <martin.gieseking@uos.de>   **
**                                                                      **
** This program is free software; you can redistribute it and/or        **
** modify it under the terms of the GNU General Public License as       **
** published by the Free Software Foundation; either version 3 of       **
** the License, or (at your option) any later version.                  **
**                                                                      **
** This program 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 General Public License for more details.                         **
**                                                                      **
** You should have received a copy of the GNU General Public License    **
** along with this program; if not, see <http://www.gnu.org/licenses/>. **
*************************************************************************/

#include "TriangularPatch.hpp"

using namespace std;

TriangularPatch::TriangularPatch (const PointVec &points, const ColorVec &colors, Color::ColorSpace cspace, int edgeflag, TriangularPatch *patch)
	: ShadingPatch(cspace)
{
	setPoints(points, edgeflag, patch);
	setColors(colors, edgeflag, patch);
}


void TriangularPatch::setPoints (const PointVec &points, int edgeflag, ShadingPatch *patch) {
	TriangularPatch *triangularPatch = nullptr;
	if (patch && patch->psShadingType() == psShadingType())
		triangularPatch = static_cast<TriangularPatch*>(patch);
	if (edgeflag > 0 && !triangularPatch)
		throw ShadingException("missing preceding data in definition of triangular patch");
	if ((edgeflag == 0 && points.size() != 3) || (edgeflag > 0 && points.size() != 1))
		throw ShadingException("invalid number of vertices in triangular patch definition");

	_points[0] = points[0];
	switch (edgeflag) {
		case 0:
			_points[1] = points[1];
			_points[2] = points[2];
			break;
		case 1:
			_points[1] = triangularPatch->_points[1];
			_points[2] = triangularPatch->_points[2];
			break;
		case 2:
			_points[1] = triangularPatch->_points[2];
			_points[2] = triangularPatch->_points[0];
	}
}


void TriangularPatch::setPoints (const DPair &p1, const DPair &p2, const DPair &p3) {
	_points[0] = p1;
	_points[1] = p2;
	_points[2] = p3;
}


void TriangularPatch::setColors (const ColorVec &colors, int edgeflag, ShadingPatch *patch) {
	TriangularPatch *triangularPatch = nullptr;
	if (patch && patch->psShadingType() == psShadingType())
		triangularPatch = static_cast<TriangularPatch*>(patch);
	if (edgeflag > 0 && !triangularPatch)
		throw ShadingException("missing preceding data in definition of triangular patch");
	if ((edgeflag == 0 && colors.size() != 3) || (edgeflag > 0 && colors.size() != 1))
		throw ShadingException("invalid number of colors in triangular patch definition");

	_colors[0] = colors[0];
	switch (edgeflag) {
		case 0:
			_colors[1] = colors[1];
			_colors[2] = colors[2];
			break;
		case 1:
			_colors[1] = triangularPatch->_colors[1];
			_colors[2] = triangularPatch->_colors[2];
			break;
		case 2:
			_colors[1] = triangularPatch->_colors[2];
			_colors[2] = triangularPatch->_colors[0];
	}
}


void TriangularPatch::setColors (const Color &c1, const Color &c2, const Color &c3) {
	_colors[0] = c1;
	_colors[1] = c2;
	_colors[2] = c3;
}


/** Returns the Cartesian coordinates for the barycentric coordinates \f$(u, v, 1-u-v)\f$
 *  of a point of the triangle, where \f$u, v \in [0,1]\f$ and \f$u+v \le 1\f$.
 *  The relation between the vertices of the triangle and their barycentric coordinates
 *  is as follows: \f$(1,0,0)=p_1, (0,1,0)=p_2, (0,0,1)=p_0\f$. */
DPair TriangularPatch::valueAt (double u, double v) const {
	return _points[0] + (_points[1]-_points[0])*u + (_points[2]-_points[0])*v;
}


/** Returns the color at a given point of the triangle. The point must be given
 *  in barycentric coordinates \f$(u, v, 1-u-v)\f$, where \f$u, v \in [0,1]\f$
 *  and \f$u+v \le 1\f$.
 *  The relation between the vertices of the triangle and their barycentric coordinates
 *  is as follows: \f$(1,0,0)=p_1, (0,1,0)=p_2, (0,0,1)=p_0\f$. */
Color TriangularPatch::colorAt (double u, double v) const {
	ColorGetter getComponents;
	ColorSetter setComponents;
	colorQueryFuncs(getComponents, setComponents);
	valarray<double> comp[3];
	for (int i=0; i < 3; i++)
		(_colors[i].*getComponents)(comp[i]);
	Color color;
	(color.*setComponents)(comp[0]*(1-u-v) + comp[1]*u + comp[2]*v);
	return color;
}


Color TriangularPatch::averageColor () const {
	return averageColor(_colors[0], _colors[1], _colors[2]);
}


/** Compute the average of three given colors depending on the assigned color space. */
Color TriangularPatch::averageColor (const Color &c1, const Color &c2, const Color &c3) const {
	ColorGetter getComponents;
	ColorSetter setComponents;
	colorQueryFuncs(getComponents, setComponents);
	valarray<double> va1, va2, va3;
	(c1.*getComponents)(va1);
	(c2.*getComponents)(va2);
	(c3.*getComponents)(va3);
	Color averageColor;
	(averageColor.*setComponents)((va1+va2+va3)/3.0);
	return averageColor;
}


/** Snaps value x to the interval [0,1]. Values lesser than or near 0 are mapped to 0, values
 *  greater than or near 1 are mapped to 1. */
static inline double snap (double x) {
	if (abs(x) < 0.001)
		return 0;
	if (abs(1-x) < 0.001)
		return 1;
	return x;
}


/** Approximate the patch by dividing it into a grid of triangular segments that are filled
 *  with the average color of the corresponding region. In order to prevent visual gaps between
 *  adjacent segments due to anti-aliasing, the flag 'overlap' can be set. It enlarges the
 *  segments so that they overlap with their right and bottom neighbors (which are drawn on
 *  top of the overlapping regions).
 *  @param[in] gridsize number of segments per row/column
 *  @param[in] overlap if true, enlarge each segment to overlap with its right and bottom neighbors
 *  @param[in] delta reduce level of detail if the segment size is smaller than the given value
 *  @param[in] callback object notified */
void TriangularPatch::approximate (int gridsize, bool overlap, double delta, Callback &callback) const {
	if (_colors[0] == _colors[1] && _colors[1] == _colors[2]) {
		GraphicsPath<double> path = getBoundaryPath();
		callback.patchSegment(path, _colors[0]);
	}
	else {
		const double inc = 1.0/gridsize;
		for (double u1=0; u1 < 1; u1=snap(u1+inc)) {
			double u2 = snap(u1+inc);
			double ou2 = (overlap && snap(u2+inc) <= 1 ? snap(u2+inc) : u2);
			for (double v1=0; snap(u1+v1) < 1; v1=snap(v1+inc)) {
				double v2 = snap(v1+inc);
				double ov2 = (overlap && snap(v2+inc) <= 1 ? snap(v2+inc) : v2);
				if (!overlap || (snap(u1+ov2) <= 1 && snap(ou2+v1) <= 1)) {
					// create triangular segments pointing in the same direction as the whole patch
					GraphicsPath<double> path;
					path.moveto(valueAt(u1, v1));
					path.lineto(valueAt(ou2, v1));
					path.lineto(valueAt(u1, ov2));
					path.closepath();
					callback.patchSegment(path, averageColor(colorAt(u1, v1), colorAt(u2, v1), colorAt(u1, v2)));
					if (snap(u2+v2) <= 1 && (!overlap || inc > delta)) {
						// create triangular segments pointing in the opposite direction as the whole patch
						path.clear();
						path.moveto(valueAt(u1, v2));
						path.lineto(valueAt(u2, v1));
						path.lineto(valueAt(u2, v2));
						path.closepath();
						callback.patchSegment(path, averageColor(colorAt(u1, v2), colorAt(u2, v1), colorAt(u2, v2)));
					}
				}
			}
		}
	}
}


GraphicsPath<double> TriangularPatch::getBoundaryPath () const {
	GraphicsPath<double> path;
	path.clear();
	path.moveto(_points[0]);
	path.lineto(_points[1]);
	path.lineto(_points[2]);
	path.closepath();
	return path;
}


BoundingBox TriangularPatch::getBBox () const {
	BoundingBox bbox;
	bbox.embed(_points[0]);
	bbox.embed(_points[1]);
	bbox.embed(_points[2]);
	return bbox;
}