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.

234 lines
5.6KB

  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. FreeEdit::FreeEdit(){
  19. enabled=false;
  20. smooth=0.0;
  21. interp_mode=LINEAR;
  22. npos=FREE_EDIT_MAX_POINTS;
  23. pos=new FreeEditPos[npos];
  24. for (int i=0;i<npos;i++){
  25. pos[i].x=pos[i].y=0;
  26. pos[i].enabled=false;
  27. };
  28. pos[0].x=0;
  29. pos[0].y=0.5;
  30. pos[0].enabled=true;
  31. pos[1].x=1;
  32. pos[1].y=0.5;
  33. pos[1].enabled=true;
  34. curve.data=NULL;
  35. curve.size=0;
  36. };
  37. void FreeEdit::deep_copy_from(const FreeEdit &other){
  38. enabled=other.enabled;
  39. smooth=other.smooth;
  40. interp_mode=other.interp_mode;
  41. npos=other.npos;
  42. pos=new FreeEditPos[npos];
  43. for (int i=0;i<npos;i++){
  44. pos[i].x=other.pos[i].x;
  45. pos[i].y=other.pos[i].y;
  46. pos[i].enabled=other.pos[i].enabled;
  47. };
  48. curve.size=other.curve.size;
  49. if (other.curve.data&&other.curve.size){
  50. curve.data=new REALTYPE[curve.size];
  51. for (int i=0;i<curve.size;i++) curve.data[i]=other.curve.data[i];
  52. }else curve.data=NULL;
  53. extreme_x=other.extreme_x;
  54. extreme_y=other.extreme_y;
  55. };
  56. FreeEdit::FreeEdit (const FreeEdit &other){
  57. deep_copy_from(other);
  58. };
  59. const FreeEdit &FreeEdit::operator=(const FreeEdit &other){
  60. if (this == &other) return *this;
  61. deep_copy_from(other);
  62. return *this;
  63. };
  64. void FreeEdit::get_curve(int datasize,REALTYPE *data,bool real_values){
  65. int npos_used=0;
  66. for (int i=0;i<npos;i++) if (is_enabled(i)) npos_used++;
  67. if (!npos_used){
  68. for (int i=0;i<datasize;i++) data[i]=(real_values?extreme_y.get_min():0.0);
  69. return;
  70. };
  71. //get the enabled points
  72. REALTYPE posx[npos_used],posy[npos_used];
  73. int k=0;
  74. for (int i=0;i<npos;i++){
  75. if (is_enabled(i)){
  76. posx[k]=get_posx(i);
  77. posy[k]=get_posy(i);
  78. k++;
  79. };
  80. };
  81. //sort the points
  82. for (int j=0;j<npos_used-1;j++){
  83. for (int i=j+1;i<npos_used;i++){
  84. if (posx[i]<posx[j]){
  85. swap(posx[i],posx[j]);
  86. swap(posy[i],posy[j]);
  87. };
  88. };
  89. };
  90. //generate the curve
  91. int p1=0,p2=1;
  92. for (int i=0;i<datasize;i++){
  93. REALTYPE x=(REALTYPE)i/(REALTYPE)datasize;
  94. while ((x>posx[p2])&&(p2<npos_used)){
  95. p1=p2;
  96. p2++;
  97. };
  98. REALTYPE px1=posx[p1];
  99. REALTYPE px2=posx[p2];
  100. REALTYPE diffx=px2-px1;
  101. REALTYPE x0=0;
  102. if (diffx>1e-5) x0=(x-px1)/diffx;
  103. if (interp_mode==COSINE) x0=(1.0-cos(x0*M_PI))*0.5;
  104. REALTYPE y=y=posy[p1]*(1.0-x0)+posy[p2]*x0;
  105. data[i]=y;
  106. };
  107. //smooth the curve
  108. if (smooth>0.01){
  109. const int max_times=4;
  110. REALTYPE a=exp(log(0.25)/(smooth*smooth*datasize*0.25));
  111. if ((a<=0.0)||(a>=1.0)) return;
  112. a=pow(a,max_times);
  113. for (k=0;k<max_times;k++){
  114. for (int i=1;i<datasize;i++) data[i]=data[i]*(1.0-a)+data[i-1]*a;
  115. for (int i=datasize-2;i>=0;i--) data[i]=data[i]*(1.0-a)+data[i+1]*a;
  116. };
  117. };
  118. if (real_values){
  119. for (int i=0;i<datasize;i++) data[i]=extreme_y.coord_to_real_value(data[i]);
  120. if (extreme_y.get_scale()==FE_DB){
  121. for (int i=0;i<datasize;i++) data[i]=dB2rap(data[i]);
  122. };
  123. };
  124. };
  125. void FreeEdit::update_curve(int size){
  126. if (curve.data) delete []curve.data;
  127. if (size<2) size=2;
  128. curve.size=size;
  129. curve.data=new REALTYPE[size];
  130. get_curve(curve.size,curve.data,true);
  131. // for(int i=0;i<size;i++) printf("_%d %g\n",i,curve.data[i]);
  132. };
  133. REALTYPE FreeEdit::get_value(REALTYPE x){
  134. if (!curve.data) {
  135. return 0.0;// update_curve();
  136. };
  137. if (extreme_x.get_scale()==FE_LOG){
  138. if (x<=0.0) x=1e-9;
  139. };
  140. // printf("%g\n",curve.data[1]);
  141. x=extreme_x.real_value_to_coord(x);
  142. if (x<0) x=0.0;
  143. else if (x>1.0) x=1.0;
  144. REALTYPE rx=x*curve.size;
  145. REALTYPE rxh=floor(rx);
  146. int k=(int)rxh;
  147. REALTYPE rxl=rx-rxh;
  148. if (k<0) k=0;
  149. if (k>(curve.size-1)) k=curve.size-1;
  150. int k1=k+1; if (k1>(curve.size-1)) k1=curve.size-1;
  151. return curve.data[k]*(1.0-rxl)+curve.data[k1]*rxl;
  152. };
  153. void FreeEdit::add2XML(XMLwrapper *xml){
  154. xml->addparbool("enabled",enabled);
  155. xml->addparreal("smooth",smooth);
  156. xml->addpar("interp_mode",interp_mode);
  157. xml->beginbranch("POINTS");
  158. for (int i=0;i<FREE_EDIT_MAX_POINTS;i++){
  159. if (!pos[i].enabled) continue;
  160. xml->beginbranch("POINT",i);
  161. xml->addparbool("enabled",pos[i].enabled);
  162. xml->addparreal("x",pos[i].x);
  163. xml->addparreal("y",pos[i].y);
  164. xml->endbranch();
  165. };
  166. xml->endbranch();
  167. xml->beginbranch("EXTREME_X");
  168. extreme_x.add2XML(xml);
  169. xml->endbranch();
  170. xml->beginbranch("EXTREME_Y");
  171. extreme_y.add2XML(xml);
  172. xml->endbranch();
  173. };
  174. void FreeEdit::getfromXML(XMLwrapper *xml){
  175. enabled=xml->getparbool("enabled",enabled);
  176. smooth=xml->getparreal("smooth",smooth);
  177. interp_mode=(INTERP_MODE)xml->getpar("interp_mode",interp_mode,0,1);
  178. if (xml->enterbranch("POINTS")){
  179. for (int i=0;i<FREE_EDIT_MAX_POINTS;i++){
  180. if (xml->enterbranch("POINT",i)){
  181. pos[i].enabled=xml->getparbool("enabled",pos[i].enabled);
  182. pos[i].x=xml->getparreal("x",pos[i].x);
  183. pos[i].y=xml->getparreal("y",pos[i].y);
  184. xml->exitbranch();
  185. };
  186. };
  187. xml->exitbranch();
  188. };
  189. if (xml->enterbranch("EXTREME_X")){
  190. extreme_x.getfromXML(xml);
  191. xml->exitbranch();
  192. };
  193. if (xml->enterbranch("EXTREME_Y")){
  194. extreme_y.getfromXML(xml);
  195. xml->exitbranch();
  196. };
  197. update_curve();
  198. };