You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
5.7KB

  1. /*
  2. Copyright (C) 2009 Nasca Octavian Paul
  3. Author: Nasca Octavian Paul
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of version 2 of the GNU General Public License
  6. as published by the Free Software Foundation.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License (version 2) for more details.
  11. You should have received a copy of the GNU General Public License (version 2)
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. #include <stdio.h>
  16. #include <math.h>
  17. #include "FreeEdit.h"
  18. #include <vector>
  19. #ifdef HAVE_PS_FREE_EDIT
  20. FreeEdit::FreeEdit(){
  21. enabled=false;
  22. smooth=0.0;
  23. interp_mode=LINEAR;
  24. npos=FREE_EDIT_MAX_POINTS;
  25. pos=new FreeEditPos[npos];
  26. for (int i=0;i<npos;i++){
  27. pos[i].x=pos[i].y=0;
  28. pos[i].enabled=false;
  29. };
  30. pos[0].x=0;
  31. pos[0].y=0.5;
  32. pos[0].enabled=true;
  33. pos[1].x=1;
  34. pos[1].y=0.5;
  35. pos[1].enabled=true;
  36. curve.data=NULL;
  37. curve.size=0;
  38. };
  39. void FreeEdit::deep_copy_from(const FreeEdit &other){
  40. enabled=other.enabled;
  41. smooth=other.smooth;
  42. interp_mode=other.interp_mode;
  43. npos=other.npos;
  44. pos=new FreeEditPos[npos];
  45. for (int i=0;i<npos;i++){
  46. pos[i].x=other.pos[i].x;
  47. pos[i].y=other.pos[i].y;
  48. pos[i].enabled=other.pos[i].enabled;
  49. };
  50. curve.size=other.curve.size;
  51. if (other.curve.data&&other.curve.size){
  52. curve.data=new REALTYPE[curve.size];
  53. for (int i=0;i<curve.size;i++) curve.data[i]=other.curve.data[i];
  54. }else curve.data=NULL;
  55. extreme_x=other.extreme_x;
  56. extreme_y=other.extreme_y;
  57. };
  58. FreeEdit::FreeEdit (const FreeEdit &other){
  59. deep_copy_from(other);
  60. };
  61. const FreeEdit &FreeEdit::operator=(const FreeEdit &other){
  62. if (this == &other) return *this;
  63. deep_copy_from(other);
  64. return *this;
  65. };
  66. void FreeEdit::get_curve(int datasize,REALTYPE *data,bool real_values){
  67. int npos_used=0;
  68. for (int i=0;i<npos;i++) if (is_enabled(i)) npos_used++;
  69. if (!npos_used){
  70. for (int i=0;i<datasize;i++) data[i]=(real_values?extreme_y.get_min():0.0f);
  71. return;
  72. };
  73. //get the enabled points
  74. std::vector<REALTYPE> posx(npos_used);
  75. std::vector<REALTYPE> posy(npos_used);
  76. int k=0;
  77. for (int i=0;i<npos;i++){
  78. if (is_enabled(i)){
  79. posx[k]=get_posx(i);
  80. posy[k]=get_posy(i);
  81. k++;
  82. };
  83. };
  84. //sort the points
  85. for (int j=0;j<npos_used-1;j++){
  86. for (int i=j+1;i<npos_used;i++){
  87. if (posx[i]<posx[j]){
  88. swap(posx[i],posx[j]);
  89. swap(posy[i],posy[j]);
  90. };
  91. };
  92. };
  93. //generate the curve
  94. int p1=0,p2=1;
  95. for (int i=0;i<datasize;i++){
  96. REALTYPE x=(REALTYPE)i/(REALTYPE)datasize;
  97. while ((x>posx[p2])&&(p2<npos_used)){
  98. p1=p2;
  99. p2++;
  100. };
  101. REALTYPE px1=posx[p1];
  102. REALTYPE px2=posx[p2];
  103. REALTYPE diffx=px2-px1;
  104. REALTYPE x0=0;
  105. if (diffx>1e-5) x0=(x-px1)/diffx;
  106. if (interp_mode==COSINE) x0=(1.0f-cos(x0*(float)M_PI))*0.5f;
  107. REALTYPE y=y=posy[p1]*(1.0f-x0)+posy[p2]*x0;
  108. data[i]=y;
  109. };
  110. //smooth the curve
  111. if (smooth>0.01){
  112. const int max_times=4;
  113. REALTYPE a=exp(log(0.25f)/(smooth*smooth*datasize*0.25f));
  114. if ((a<=0.0f)||(a>=1.0f)) return;
  115. a=pow(a,max_times);
  116. for (k=0;k<max_times;k++){
  117. for (int i=1;i<datasize;i++) data[i]=data[i]*(1.0f-a)+data[i-1]*a;
  118. for (int i=datasize-2;i>=0;i--) data[i]=data[i]*(1.0f-a)+data[i+1]*a;
  119. };
  120. };
  121. if (real_values){
  122. for (int i=0;i<datasize;i++) data[i]=extreme_y.coord_to_real_value(data[i]);
  123. if (extreme_y.get_scale()==FE_DB){
  124. for (int i=0;i<datasize;i++) data[i]=(float)dB2rap(data[i]);
  125. };
  126. };
  127. };
  128. void FreeEdit::update_curve(int size){
  129. if (curve.data) delete []curve.data;
  130. if (size<2) size=2;
  131. curve.size=size;
  132. curve.data=new REALTYPE[size];
  133. get_curve(curve.size,curve.data,true);
  134. // for(int i=0;i<size;i++) printf("_%d %g\n",i,curve.data[i]);
  135. };
  136. REALTYPE FreeEdit::get_value(REALTYPE x){
  137. if (!curve.data) {
  138. return 0.0;// update_curve();
  139. };
  140. if (extreme_x.get_scale()==FE_LOG){
  141. if (x<=0.0f) x=1e-9f;
  142. };
  143. // printf("%g\n",curve.data[1]);
  144. x=extreme_x.real_value_to_coord(x);
  145. if (x<0) x=0.0;
  146. else if (x>1.0) x=1.0;
  147. REALTYPE rx=x*curve.size;
  148. REALTYPE rxh=floor(rx);
  149. int k=(int)rxh;
  150. REALTYPE rxl=rx-rxh;
  151. if (k<0) k=0;
  152. if (k>(curve.size-1)) k=curve.size-1;
  153. int k1=k+1; if (k1>(curve.size-1)) k1=curve.size-1;
  154. return curve.data[k]*(1.0f-rxl)+curve.data[k1]*rxl;
  155. };
  156. /*
  157. void FreeEdit::add2XML(XMLwrapper *xml){
  158. xml->addparbool("enabled",enabled);
  159. xml->addparreal("smooth",smooth);
  160. xml->addpar("interp_mode",interp_mode);
  161. xml->beginbranch("POINTS");
  162. for (int i=0;i<FREE_EDIT_MAX_POINTS;i++){
  163. if (!pos[i].enabled) continue;
  164. xml->beginbranch("POINT",i);
  165. xml->addparbool("enabled",pos[i].enabled);
  166. xml->addparreal("x",pos[i].x);
  167. xml->addparreal("y",pos[i].y);
  168. xml->endbranch();
  169. };
  170. xml->endbranch();
  171. xml->beginbranch("EXTREME_X");
  172. extreme_x.add2XML(xml);
  173. xml->endbranch();
  174. xml->beginbranch("EXTREME_Y");
  175. extreme_y.add2XML(xml);
  176. xml->endbranch();
  177. };
  178. void FreeEdit::getfromXML(XMLwrapper *xml){
  179. enabled=xml->getparbool("enabled",enabled);
  180. smooth=xml->getparreal("smooth",smooth);
  181. interp_mode=(INTERP_MODE)xml->getpar("interp_mode",interp_mode,0,1);
  182. if (xml->enterbranch("POINTS")){
  183. for (int i=0;i<FREE_EDIT_MAX_POINTS;i++){
  184. if (xml->enterbranch("POINT",i)){
  185. pos[i].enabled=xml->getparbool("enabled",pos[i].enabled);
  186. pos[i].x=xml->getparreal("x",pos[i].x);
  187. pos[i].y=xml->getparreal("y",pos[i].y);
  188. xml->exitbranch();
  189. };
  190. };
  191. xml->exitbranch();
  192. };
  193. if (xml->enterbranch("EXTREME_X")){
  194. extreme_x.getfromXML(xml);
  195. xml->exitbranch();
  196. };
  197. if (xml->enterbranch("EXTREME_Y")){
  198. extreme_y.getfromXML(xml);
  199. xml->exitbranch();
  200. };
  201. update_curve();
  202. };
  203. */
  204. #endif