summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 19f4d9e)
raw | patch | inline | side by side (parent: 19f4d9e)
author | johanengelen <johanengelen@users.sourceforge.net> | |
Sun, 13 Apr 2008 21:00:01 +0000 (21:00 +0000) | ||
committer | johanengelen <johanengelen@users.sourceforge.net> | |
Sun, 13 Apr 2008 21:00:01 +0000 (21:00 +0000) |
src/2geom/svg-elliptical-arc.cpp | [new file with mode: 0644] | patch | blob |
diff --git a/src/2geom/svg-elliptical-arc.cpp b/src/2geom/svg-elliptical-arc.cpp
--- /dev/null
@@ -0,0 +1,209 @@
+/*\r
+ * SVG Elliptical Arc Class\r
+ *\r
+ * Copyright 2008 Marco Cecchetti <mrcekets at gmail.com>\r
+ *\r
+ * This library is free software; you can redistribute it and/or\r
+ * modify it either under the terms of the GNU Lesser General Public\r
+ * License version 2.1 as published by the Free Software Foundation\r
+ * (the "LGPL") or, at your option, under the terms of the Mozilla\r
+ * Public License Version 1.1 (the "MPL"). If you do not alter this\r
+ * notice, a recipient may use your version of this file under either\r
+ * the MPL or the LGPL.\r
+ *\r
+ * You should have received a copy of the LGPL along with this library\r
+ * in the file COPYING-LGPL-2.1; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
+ * You should have received a copy of the MPL along with this library\r
+ * in the file COPYING-MPL-1.1\r
+ *\r
+ * The contents of this file are subject to the Mozilla Public License\r
+ * Version 1.1 (the "License"); you may not use this file except in\r
+ * compliance with the License. You may obtain a copy of the License at\r
+ * http://www.mozilla.org/MPL/\r
+ *\r
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY\r
+ * OF ANY KIND, either express or implied. See the LGPL or the MPL for\r
+ * the specific language governing rights and limitations.\r
+ */\r
+\r
+\r
+#include "path.h"\r
+\r
+\r
+namespace Geom\r
+{\r
+\r
+D2<SBasis> SVGEllipticalArc::toSBasis() const\r
+{\r
+ // the interval of parametrization has to be [0,1]\r
+ Coord et = start_angle() + ( sweep_flag() ? sweep_angle() : -sweep_angle() );\r
+ Linear param(start_angle(), et);\r
+ Coord cos_rot_angle = std::cos(rotation_angle());\r
+ Coord sin_rot_angle = std::sin(rotation_angle());\r
+ // order = 4 seems to be enough to get a perfect looking elliptical arc\r
+ // should it be choosen in function of the arc length anyway ?\r
+ // or maybe a user settable parameter: toSBasis(unsigned int order) ?\r
+ SBasis arc_x = ray(X) * cos(param,4);\r
+ SBasis arc_y = ray(Y) * sin(param,4);\r
+ D2<SBasis> arc;\r
+ arc[0] = arc_x * cos_rot_angle - arc_y * sin_rot_angle + Linear(center(X),center(X));\r
+ arc[1] = arc_x * sin_rot_angle + arc_y * cos_rot_angle + Linear(center(Y),center(Y));\r
+ return arc;\r
+}\r
+\r
+double SVGEllipticalArc::valueAt(Coord t, Dim2 d) const\r
+{\r
+ Coord tt = from_01_to_02PI(t);\r
+ double sin_rot_angle = std::sin(rotation_angle());\r
+ double cos_rot_angle = std::cos(rotation_angle());\r
+ if ( d == X )\r
+ {\r
+ return ray(X) * cos_rot_angle * std::cos(tt) \r
+ - ray(Y) * sin_rot_angle * std::sin(tt) \r
+ + center(X);\r
+ }\r
+ else\r
+ {\r
+ return ray(X) * sin_rot_angle * std::cos(tt) \r
+ + ray(Y) * cos_rot_angle * std::sin(tt) \r
+ + center(Y);\r
+ }\r
+}\r
+\r
+\r
+Curve* SVGEllipticalArc::portion(double f, double t) const \r
+{\r
+ if (f < 0) f = 0;\r
+ if (f > 1) f = 1;\r
+ if (t < 0) t = 0;\r
+ if (t > 1) t = 1;\r
+ SVGEllipticalArc* arc = new SVGEllipticalArc( *this );\r
+ arc->m_initial_point = pointAt(f);\r
+ arc->m_final_point = pointAt(t);\r
+ double sa = sweep_flag() ? sweep_angle() : -sweep_angle();\r
+ arc->m_start_angle = m_start_angle + sa * f;\r
+ if ( !(arc->m_start_angle < 2*M_PI) )\r
+ arc->m_start_angle -= 2*M_PI;\r
+ if ( !(arc->m_start_angle > 0) )\r
+ arc->m_start_angle += 2*M_PI;\r
+ arc->m_end_angle = m_start_angle + sa * t;\r
+ if ( !(arc->m_end_angle < 2*M_PI) )\r
+ arc->m_end_angle -= 2*M_PI;\r
+ if ( !(arc->m_end_angle > 0) )\r
+ arc->m_end_angle += 2*M_PI;\r
+ if ( f > t ) arc->m_sweep = !sweep_flag();\r
+ if ( large_arc_flag() && (arc->sweep_angle() < M_PI) )\r
+ arc->m_large_arc = false;\r
+ return arc;\r
+}\r
+\r
+// NOTE: doesn't work with 360 deg arcs\r
+void SVGEllipticalArc::calculate_center_and_extreme_angles()\r
+{\r
+ double sin_rot_angle = std::sin(rotation_angle());\r
+ double cos_rot_angle = std::cos(rotation_angle());\r
+\r
+ Point sp = sweep_flag() ? initialPoint() : finalPoint();\r
+ Point ep = sweep_flag() ? finalPoint() : initialPoint();\r
+\r
+ Matrix m( ray(X) * cos_rot_angle, ray(X) * sin_rot_angle,\r
+ -ray(Y) * sin_rot_angle, ray(Y) * cos_rot_angle,\r
+ 0, 0 );\r
+ Matrix im = m.inverse();\r
+ Point sol = (ep - sp) * im;\r
+ double half_sum_angle = std::atan2(-sol[X], sol[Y]);\r
+ double half_diff_angle;\r
+ if ( are_near(std::fabs(half_sum_angle), M_PI/2) )\r
+ {\r
+ double anti_sgn_hsa = (half_sum_angle > 0) ? -1 : 1;\r
+ double arg = anti_sgn_hsa * sol[X] / 2;\r
+ // if |arg| is a little bit > 1 acos returns nan\r
+ if ( are_near(arg, 1) )\r
+ half_diff_angle = 0;\r
+ else if ( are_near(arg, -1) )\r
+ half_diff_angle = M_PI;\r
+ else\r
+ {\r
+ assert( -1 < arg && arg < 1 );\r
+ // if it fails \r
+ // => there is no ellipse that satisfies the given constraints\r
+ half_diff_angle = std::acos( arg );\r
+ }\r
+\r
+ half_diff_angle = M_PI/2 - half_diff_angle;\r
+ }\r
+ else\r
+ {\r
+ double arg = sol[Y] / ( 2 * std::cos(half_sum_angle) );\r
+ // if |arg| is a little bit > 1 asin returns nan\r
+ if ( are_near(arg, 1) ) \r
+ half_diff_angle = M_PI/2;\r
+ else if ( are_near(arg, -1) )\r
+ half_diff_angle = -M_PI/2;\r
+ else\r
+ {\r
+ assert( -1 < arg && arg < 1 ); \r
+ // if it fails \r
+ // => there is no ellipse that satisfies the given constraints\r
+ half_diff_angle = std::asin( arg );\r
+ }\r
+ }\r
+\r
+ if ( ( m_large_arc && half_diff_angle > 0 ) \r
+ || (!m_large_arc && half_diff_angle < 0 ) )\r
+ {\r
+ half_diff_angle = -half_diff_angle;\r
+ }\r
+ if ( half_sum_angle < 0 ) half_sum_angle += 2*M_PI;\r
+ if ( half_diff_angle < 0 ) half_diff_angle += M_PI;\r
+ \r
+ m_start_angle = half_sum_angle - half_diff_angle;\r
+ m_end_angle = half_sum_angle + half_diff_angle;\r
+ // 0 <= m_start_angle, m_end_angle < 2PI\r
+ if ( m_start_angle < 0 ) m_start_angle += 2*M_PI;\r
+ if( !(m_end_angle < 2*M_PI) ) m_end_angle -= 2*M_PI;\r
+ sol[0] = std::cos(m_start_angle);\r
+ sol[1] = std::sin(m_start_angle);\r
+ m_center = sp - sol * m;\r
+ if ( !sweep_flag() )\r
+ {\r
+ double angle = m_start_angle;\r
+ m_start_angle = m_end_angle;\r
+ m_end_angle = angle;\r
+ }\r
+}\r
+\r
+Coord SVGEllipticalArc::from_01_to_02PI(Coord t) const\r
+{\r
+ if ( sweep_flag() )\r
+ {\r
+ Coord angle = start_angle() + sweep_angle() * t;\r
+ if ( !(angle < 2*M_PI) )\r
+ angle -= 2*M_PI;\r
+ return angle;\r
+ }\r
+ else\r
+ {\r
+ Coord angle = start_angle() - sweep_angle() * t;\r
+ if ( angle < 0 ) angle += 2*M_PI;\r
+ return angle;\r
+ }\r
+}\r
+\r
+\r
+} // end namespace Geom\r
+\r
+\r
+/*\r
+ Local Variables:\r
+ mode:c++\r
+ c-file-style:"stroustrup"\r
+ c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))\r
+ indent-tabs-mode:nil\r
+ fill-column:99\r
+ End:\r
+*/\r
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :\r
+\r
+\r