00001 // $Id: pressure.cpp 1282 2006-06-09 09:46:49Z alex $ 00002 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE 00003 ================================XARAHEADERSTART=========================== 00004 00005 Xara LX, a vector drawing and manipulation program. 00006 Copyright (C) 1993-2006 Xara Group Ltd. 00007 Copyright on certain contributions may be held in joint with their 00008 respective authors. See AUTHORS file for details. 00009 00010 LICENSE TO USE AND MODIFY SOFTWARE 00011 ---------------------------------- 00012 00013 This file is part of Xara LX. 00014 00015 Xara LX is free software; you can redistribute it and/or modify it 00016 under the terms of the GNU General Public License version 2 as published 00017 by the Free Software Foundation. 00018 00019 Xara LX and its component source files are distributed in the hope 00020 that it will be useful, but WITHOUT ANY WARRANTY; without even the 00021 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00022 See the GNU General Public License for more details. 00023 00024 You should have received a copy of the GNU General Public License along 00025 with Xara LX (see the file GPL in the root directory of the 00026 distribution); if not, write to the Free Software Foundation, Inc., 51 00027 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00028 00029 00030 ADDITIONAL RIGHTS 00031 ----------------- 00032 00033 Conditional upon your continuing compliance with the GNU General Public 00034 License described above, Xara Group Ltd grants to you certain additional 00035 rights. 00036 00037 The additional rights are to use, modify, and distribute the software 00038 together with the wxWidgets library, the wxXtra library, and the "CDraw" 00039 library and any other such library that any version of Xara LX relased 00040 by Xara Group Ltd requires in order to compile and execute, including 00041 the static linking of that library to XaraLX. In the case of the 00042 "CDraw" library, you may satisfy obligation under the GNU General Public 00043 License to provide source code by providing a binary copy of the library 00044 concerned and a copy of the license accompanying it. 00045 00046 Nothing in this section restricts any of the rights you have under 00047 the GNU General Public License. 00048 00049 00050 SCOPE OF LICENSE 00051 ---------------- 00052 00053 This license applies to this program (XaraLX) and its constituent source 00054 files only, and does not necessarily apply to other Xara products which may 00055 in part share the same code base, and are subject to their own licensing 00056 terms. 00057 00058 This license does not apply to files in the wxXtra directory, which 00059 are built into a separate library, and are subject to the wxWindows 00060 license contained within that directory in the file "WXXTRA-LICENSE". 00061 00062 This license does not apply to the binary libraries (if any) within 00063 the "libs" directory, which are subject to a separate license contained 00064 within that directory in the file "LIBS-LICENSE". 00065 00066 00067 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS 00068 ---------------------------------------------- 00069 00070 Subject to the terms of the GNU Public License (see above), you are 00071 free to do whatever you like with your modifications. However, you may 00072 (at your option) wish contribute them to Xara's source tree. You can 00073 find details of how to do this at: 00074 http://www.xaraxtreme.org/developers/ 00075 00076 Prior to contributing your modifications, you will need to complete our 00077 contributor agreement. This can be found at: 00078 http://www.xaraxtreme.org/developers/contribute/ 00079 00080 Please note that Xara will not accept modifications which modify any of 00081 the text between the start and end of this header (marked 00082 XARAHEADERSTART and XARAHEADEREND). 00083 00084 00085 MARKS 00086 ----- 00087 00088 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara 00089 designs are registered or unregistered trademarks, design-marks, and/or 00090 service marks of Xara Group Ltd. All rights in these marks are reserved. 00091 00092 00093 Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK. 00094 http://www.xara.com/ 00095 00096 =================================XARAHEADEREND============================ 00097 */ 00098 // Pressure - functions for smoothing freehand pressure data 00099 00100 #include "camtypes.h" 00101 #include "pressure.h" 00102 00103 //#include "paths.h" - in camtypes.h [AUTOMATICALLY REMOVED] 00104 #include "pathtrap.h" 00105 #include "valfunc.h" 00106 00107 DECLARE_SOURCE("$Revision: 1282 $"); 00108 00109 CC_IMPLEMENT_MEMDUMP(PressureSmoother, CC_CLASS_MEMDUMP) 00110 00111 00112 #define new CAM_DEBUG_NEW 00113 00114 00115 /******************************************************************************************** 00116 00117 > PressureSmoother::PressureSmoother() 00118 00119 Author; Jason 00120 Created: 30/1/97 00121 Purpose: Constructor 00122 00123 ********************************************************************************************/ 00124 00125 PressureSmoother::PressureSmoother() 00126 { 00127 } 00128 00129 00130 00131 /******************************************************************************************** 00132 00133 > PressureSmoother::~PressureSmoother() 00134 00135 Author; Jason 00136 Created: 30/1/97 00137 Purpose: Destructor 00138 00139 ********************************************************************************************/ 00140 00141 PressureSmoother::~PressureSmoother() 00142 { 00143 } 00144 00145 00146 00147 /******************************************************************************************** 00148 00149 > ValueFunction *PressureSmoother::Smooth(Path *pSourceData, INT32 LineWidth) 00150 00151 Author: Jason_Williams (Xara Group Ltd) <camelotdev@xara.com> 00152 Created: 21/1/97 00153 Inputs: pSourceData - The path containing the pressure data to smooth. 00154 The pressure information should be held in the Width channel 00155 in the ExtraInfo of the path 00156 00157 LineWidth - The maximum "radius" of the line (half the line width) which 00158 will be used when stroking this path. 00159 00160 Returns: NULL if it failed (no error is set), else 00161 a pointer to a new ValueFunction object which represents the pressure 00162 information form this path. The caller is responsible for deleting this 00163 new object when they're finished with it. 00164 00165 Purpose: Reads raw recorded pressure information from a path and smooths it, 00166 creating a new ValueFunction object representing the pressure function. 00167 00168 Notes: Smoothing works as follows: 00169 1) Create a graph of the pressure samples, with pressure (y) versus 00170 position along the path (x). Store this graph in a Path. 00171 00172 2) Smooth the straight line segments in this graph into curve segments 00173 At present, this is done by replacing everything between 2 local 00174 minima/maxima by an S-shaped curve. This interpolation can be done by 00175 a ValueFunctionPressureS object in real-time, so we create one of them. 00176 00177 ********************************************************************************************/ 00178 00179 ValueFunction *PressureSmoother::Smooth(Path *pSourceData, INT32 LineWidth) 00180 { 00181 ERROR3IF(pSourceData == NULL, "Illegal NULL param"); 00182 00183 // Find the recorded pressure values (if any) 00184 PathWidth *pWidthArray = pSourceData->GetWidthArray(); 00185 if (pWidthArray == NULL) 00186 return(NULL); 00187 00188 const INT32 NumCoords = pSourceData->GetNumCoords(); 00189 DocCoord *pCoords = pSourceData->GetCoordArray(); 00190 00191 if (NumCoords < 2) 00192 return(NULL); 00193 00194 // Create a ValueFunction to return. We use an "S" curve interpolated pressure function 00195 ValueFunctionPressure *pValFunc = new ValueFunctionPressureS; 00196 if (pValFunc == NULL) 00197 return(NULL); 00198 00199 // Calculate the maximum pressure value recorded. This should be EXTRAVALUEMAX 00200 // but sometimes we seem to get bigger values. 00201 INT32 MaxPressure = pWidthArray[0]; 00202 for (INT32 i = 1; i < NumCoords; i++) 00203 { 00204 if (pCoords[i].y > MaxPressure) 00205 MaxPressure = pWidthArray[i]; 00206 } 00207 00208 // If this pressure was smaller than EXTRAVALUEMAX, then we use that as the maximum, 00209 // because we don't want to scale thin strokes up to max width! 00210 if (MaxPressure < (INT32)EXTRAVALUEMAX) 00211 MaxPressure = EXTRAVALUEMAX; 00212 00213 float Position = 0.0f; 00214 float LastPosition = Position; 00215 INT32 Diff = 0; 00216 INT32 LastDiff = pWidthArray[1] - pWidthArray[0]; 00217 00218 pValFunc->AddPressure(Position, (float)pWidthArray[0] / (float)MaxPressure); 00219 00220 // --- Now loop through the pressure samples, scaling them into the correct range, calculating 00221 // proper "position" values for them, and smoothing the data (by replacing all points between 00222 // minima and maxima with s-shaped interpolated segments) 00223 for (INT32 Index = 1; Index < NumCoords - 1; Index++) 00224 { 00225 // Record the pressure sample. We chuck away all samples between minima/maxima 00226 // as we go, and also wheedle out any zero-length sections that might crop up. 00227 // Then the ValueFunction will interpolate between the values as it sees fit (with S-shaped curves) 00228 if (Position > LastPosition) 00229 { 00230 LastPosition = Position; 00231 00232 Diff = pWidthArray[Index] - pWidthArray[Index - 1]; 00233 if ((Diff > 0 && LastDiff <= 0) || (Diff < 0 && LastDiff >= 0)) 00234 { 00235 // We have found a min/maximum. Record this point. 00236 pValFunc->AddPressure(Position, (float)pWidthArray[Index] / (float)MaxPressure); 00237 LastDiff = Diff; 00238 } 00239 } 00240 00241 // Work out approximate "travel" along the path so each width sample has a known position 00242 // [This should be calculated in the same way as in TrapEdgeList::ProcessEdgePositions, pathtrap.cpp] 00243 00244 #if TRUE 00245 // This now simply calculates distance down the centreline. 00246 // Much faster & simpler, and it turns out it gives better results after all! 00247 const double dx = (double) (pCoords[Index-1].x - pCoords[Index].x); 00248 const double dy = (double) (pCoords[Index-1].y - pCoords[Index].y); 00249 Position += (float) sqrt((dx * dx) + (dy * dy)); 00250 #else 00251 /* 00252 // Calculate the line normals to the left and right of the point 00253 NormCoord Normal1; 00254 NormCoord Normal2; 00255 Normal1.SetNormalToLine(pCoords[Index], pCoords[Index - 1]); 00256 Normal2.SetNormalToLine(pCoords[Index + 1], pCoords[Index]); 00257 00258 // Calculate the "left" parallel edge 00259 DocCoord P1( pCoords[Index-1].x + (INT32)((double) LineWidth * Normal1.x), 00260 pCoords[Index-1].y + (INT32)((double) LineWidth * Normal1.y)); 00261 DocCoord P2( pCoords[Index ].x + (INT32)((double) LineWidth * Normal2.x), 00262 pCoords[Index ].y + (INT32)((double) LineWidth * Normal2.y)); 00263 double dx = P1.x - P2.x; 00264 double dy = P1.y - P2.y; 00265 double LeftTravel = sqrt((dx * dx) + (dy * dy)); 00266 00267 // Calculate the "right" parallel edge 00268 P1 = DocCoord( pCoords[Index-1].x - (INT32)((double) LineWidth * Normal1.x), 00269 pCoords[Index-1].y - (INT32)((double) LineWidth * Normal1.y)); 00270 P2 = DocCoord( pCoords[Index ].x - (INT32)((double) LineWidth * Normal2.x), 00271 pCoords[Index ].y - (INT32)((double) LineWidth * Normal2.y)); 00272 dx = P1.x - P2.x; 00273 dy = P1.y - P2.y; 00274 double RightTravel = sqrt((dx * dx) + (dy * dy)); 00275 00276 // And increment Position by the larger of the 2 travel distances 00277 Position += (float)( (LeftTravel > RightTravel) ? LeftTravel : RightTravel ); 00278 */ 00279 #endif 00280 } 00281 00282 // And always add a knot at the very end of the curve 00283 pValFunc->AddPressure(Position, (float)pWidthArray[NumCoords - 1] / (float)MaxPressure); 00284 00285 pValFunc->NormalisePositions(); // And normalise all positions to lie between 0.0 and 1.0 00286 00287 return(pValFunc); 00288 } 00289