------------------------------------------------------------------------------
| 2223 | |
| 2224 | //------------------------------------------------------------------------------ |
| 2225 | void vtkSVGContextDevice2D::DrawTriangleGradient(const vtkVector2f& p1, const vtkColor4ub& c1, |
| 2226 | const vtkVector2f& p2, const vtkColor4ub& c2, const vtkVector2f& p3, const vtkColor4ub& c3, |
| 2227 | bool useAlpha) |
| 2228 | { |
| 2229 | // If the colors are more or less the same, go ahead and draw this triangle. |
| 2230 | // Same if the triangle is small enough to fit on a single pixel. |
| 2231 | if (this->AreaLessThanTolerance(p1, p2, p3) || this->ColorsAreClose(c1, c2, c3, useAlpha)) |
| 2232 | { |
| 2233 | YConverter y(this->CanvasHeight); |
| 2234 | const vtkColor4ub aveColor = { static_cast<unsigned char>( |
| 2235 | static_cast<int>(c1[0] + c2[0] + c3[0]) / 3), |
| 2236 | static_cast<unsigned char>(static_cast<int>(c1[1] + c2[1] + c3[1]) / 3), |
| 2237 | static_cast<unsigned char>(static_cast<int>(c1[2] + c2[2] + c3[2]) / 3), |
| 2238 | static_cast<unsigned char>(static_cast<int>(c1[3] + c2[3] + c3[3]) / 3) }; |
| 2239 | vtkNew<vtkXMLDataElement> polygon; |
| 2240 | this->ActiveNode->AddNestedElement(polygon); |
| 2241 | polygon->SetName("polygon"); |
| 2242 | polygon->SetAttribute("fill", ColorToString(aveColor.GetData()).c_str()); |
| 2243 | if (useAlpha && aveColor[3] != 255) |
| 2244 | { |
| 2245 | polygon->SetFloatAttribute("fill-opacity", aveColor[3] / 255.f); |
| 2246 | } |
| 2247 | |
| 2248 | // This should disable antialiasing on supported viewers (works on webkit). |
| 2249 | // Helps prevent visible boundaries between polygons: |
| 2250 | polygon->SetAttribute("shape-rendering", "crispEdges"); |
| 2251 | |
| 2252 | std::ostringstream points; |
| 2253 | points << p1[0] << "," << y(p1[1]) << " " << p2[0] << "," << y(p2[1]) << " " << p3[0] << "," |
| 2254 | << y(p3[1]); |
| 2255 | polygon->SetAttribute("points", points.str().c_str()); |
| 2256 | |
| 2257 | return; |
| 2258 | } |
| 2259 | |
| 2260 | // Otherwise, subdivide into 4 triangles: |
| 2261 | // v1 |
| 2262 | // + |
| 2263 | // /| |
| 2264 | // / | |
| 2265 | // / | |
| 2266 | // / | |
| 2267 | // v12 +----+ v13 |
| 2268 | // /| /| |
| 2269 | // / | / | |
| 2270 | // / | / | |
| 2271 | // / |/ | |
| 2272 | // +----+----+ |
| 2273 | // v2 v23 v3 |
| 2274 | const vtkVector2f p12 = (p1 + p2) * 0.5; |
| 2275 | const vtkVector2f p23 = (p2 + p3) * 0.5; |
| 2276 | const vtkVector2f p13 = (p1 + p3) * 0.5; |
| 2277 | const vtkColor4ub c12 = { static_cast<unsigned char>(static_cast<int>(c1[0] + c2[0]) / 2), |
| 2278 | static_cast<unsigned char>(static_cast<int>(c1[1] + c2[1]) / 2), |
| 2279 | static_cast<unsigned char>(static_cast<int>(c1[2] + c2[2]) / 2), |
| 2280 | static_cast<unsigned char>(static_cast<int>(c1[3] + c2[3]) / 2) }; |
| 2281 | const vtkColor4ub c23 = { static_cast<unsigned char>(static_cast<int>(c2[0] + c3[0]) / 2), |
| 2282 | static_cast<unsigned char>(static_cast<int>(c2[1] + c3[1]) / 2), |
no test coverage detected