//pattern.cpp #include "stdafx.h" #include #include #include #include #include "dst.h" #include "csv.h" #include "pattern.h" #include "helpers.h" //#include "analyze.h" //#include "paint.h" #include "Messaging.h" //number of stitches to allocate at a time #define BLOCK_ALLOCATE_SIZE 1000 ////////////////////////////////////////////////////////////////////////// // pattern_config class void pattern_config::clear() { scale=1.0; format=0; }; ////////////////////////////////////////////////////////////////////////// // pattern class void pattern::clear() { //clear out the pattern, but leave allocated space (except variable strings) int i; for(i=0; i=allocated_variables) { allocated_variables+=32; // allocate more space for lists of pointers variable_name=(char **)realloc(variable_name,sizeof(char *)*allocated_variables); variable_value=(char **)realloc(variable_value,sizeof(char *)*allocated_variables); if (variable_name ==0 || variable_value==0) { fprintf(stderr,"ERROR: failed to alloc %d variable_name and variable_value pointers in initialize_pattern.\n",allocated_variables); exit(-1); }; }; variable_name[num_variables]=stralloccopy(name); variable_value[num_variables]=stralloccopy(value); num_variables++; }; char *pattern::get_variable(const char *name) { int i; for (i=0; i0) { x = stitchlist[stitches-1].xx + dx; y = stitchlist[stitches-1].yy + dy; } else { // the list is empty so assume starting location is 0,0 x = dx; y = dy; }; return(add_stitch_abs(x,y,flags)); }; int pattern::add_stitch_copy (struct stitch src) { return(add_stitch_abs(src.xx,src.yy,src.flags)); }; // Delete a stitch from the middle of the list. int pattern::delete_stitch (int number) { int i; for (i=number; inumber; i--) { stitchlist[i]=stitchlist[i-1]; }; stitchlist[number]=temp; return(stitches); }; // A simple list of the stitches, handy for debugging. (obsoleted by csv_write) /* void pattern::print_stitchlist (char *filename) { double xx,yy,dx,dy; int flags; int i; FILE *fout; double length; fout = fopen(filename,"w"); if (fout==0) { fprintf(stderr, "Error opening %s for write\n",filename); //exit(-1); }; xx=yy=0.0; for (i=0;i0 && stitchlist[i-1].flags!=NORMAL) length=0.0; //can't count first normal stitch; switch(stitchlist[i].flags) { case NORMAL: real_stitches++; if(length>max_stitchlength) {max_stitchlength=length; number_of_maxlength_stitches=0;}; if(length==max_stitchlength) number_of_maxlength_stitches++; if(length>0 && lengthmaxx) maxx=xx; if(yymaxy) maxy=yy; break; case JUMP: jump_stitches++; break; case STOP: colors++; break; case END: break; default: unknown_stitches++; break; }; }; //second pass to fill bins now that we know max stitch length #define NUMBINS 10 int bin[NUMBINS+1]; for (i=0;i<=NUMBINS;i++) { bin[i]=0; }; for (i=0;i0 && stitchlist[i-1].flags==NORMAL && stitchlist[i].flags == NORMAL) { length=sqrt(dx * dx + dy * dy); bin[int(floor(NUMBINS*length/max_stitchlength))]++; }; }; char buf[1000]; messages.clear(); sprintf(buf,"Design name: %s\n",get_variable("design_name")); messages.add(buf); sprintf(buf,"Number of stitch entries: %d\n",stitches); messages.add(buf); sprintf(buf," Real stitches: %d\n",real_stitches); messages.add(buf); sprintf(buf," Jump stitches: %d\n",jump_stitches); messages.add(buf); sprintf(buf," Colors: %d\n",colors); messages.add(buf); sprintf(buf," Unknown stitches: %d\n",unknown_stitches); messages.add(buf); sprintf(buf,"Design width x height = %.1lf x %.1lf\n",maxx-minx,maxy-miny); messages.add(buf); sprintf(buf,"Center of design = %.1lf,%.1lf\n",(maxx+minx)/2.0,(maxy+miny)/2.0); messages.add(buf); sprintf(buf,"Total length of stitches: %.2lf\n",total_stitchlength); messages.add(buf); sprintf(buf,"Maximum stitch length: %.2lf (%d at this length)\n",max_stitchlength, number_of_maxlength_stitches); messages.add(buf); sprintf(buf,"Minimum stitch length: %.2lf (%d at this length)\n",min_stitchlength, number_of_minlength_stitches); messages.add(buf); sprintf(buf,"Average length of stitches: %.2lf\n",total_stitchlength/real_stitches); messages.add(buf); sprintf(buf,"Stitch length distribution:\n"); messages.add(buf); for (i=0;idim.right) dim.right=xx; if(yydim.bottom) dim.bottom=yy; }; return (dim); }; void pattern::calculate_angles(void) { double xx,yy,dx,dy; int flags; int i; xx=yy=0.0; double pi = 3.1415926535; for (i=0;iGetFileName(); CString extension = filename.Right(3); if (extension.CompareNoCase("dst")==0) { status("Loading DST file:",filename); // read_dst(fp); status("Done loading",filename); } else if (extension.CompareNoCase("csv")==0) { status("Loading CSV file:",filename); read_csv(fp); status("Done loading",filename); } else { status("Error: Unknown file type",extension); return(FALSE); }; status_updateall(this); //force_refresh_all(); return(TRUE); }; int pattern::save(CFile *fp) { CString filename = fp->GetFileName(); CString extension = filename.Right(3); if (extension.CompareNoCase("dst")==0) { status("Saving DST file:",filename); // write_dst(fp); status("Done saving",filename); } else if (extension.CompareNoCase("csv")==0) { status("Saving CSV file:",filename); write_csv(fp); status("Done saving",filename); } else { status("Error: Unknown file type",extension); return(FALSE); }; status_updateall(this); //force_refresh_all(); return(TRUE); }; */