bool ChangeSet::move_helper(int pos, int length, int to) { if (hasOverlap(pos, length) || hasOverlap(to, 0) || overlaps(pos, length, to, 0)) m_error = true; EditOp cmd(EditOp::Move); cmd.pos1 = pos; cmd.length1 = length; cmd.pos2 = to; m_operationList += cmd; return !m_error; }
bool ChangeSet::flip_helper(int pos1, int length1, int pos2, int length2) { if (hasOverlap(pos1, length1) || hasOverlap(pos2, length2) || overlaps(pos1, length1, pos2, length2)) m_error = true; EditOp cmd(EditOp::Flip); cmd.pos1 = pos1; cmd.length1 = length1; cmd.pos2 = pos2; cmd.length2 = length2; m_operationList += cmd; return !m_error; }
bool ChangeSet::remove_helper(int pos, int length) { if (hasOverlap(pos, length)) m_error = true; EditOp cmd(EditOp::Remove); cmd.pos1 = pos; cmd.length1 = length; m_operationList += cmd; return !m_error; }
bool ChangeSet::insert(int pos, const QString &text) { if (hasOverlap(pos, 0)) m_error = true; EditOp cmd(EditOp::Insert); cmd.pos1 = pos; cmd.text = text; m_operationList += cmd; return !m_error; }
bool ChangeSet::replace_helper(int pos, int length, const QString &replacement) { if (hasOverlap(pos, length)) m_error = true; EditOp cmd(EditOp::Replace); cmd.pos1 = pos; cmd.length1 = length; cmd.text = replacement; m_operationList += cmd; return !m_error; }
// Adds a chunk to the model int model_add_chunk(struct model *md, const struct chunk *ck) { /*printf("(left, top, lowest layer) = x: %d y: %d z: %d \n", ck->x, ck->y, ck->z); printf("Dimensions of chunk width: %d height: %d depth: %d \n", ck->width, ck->height, ck->depth); printf("Material id: %d \n", ck->mat); printf("MD_WIDTH is %d\n", md->width); printf("numchunks is %d\n", md->nchunks);*/ printf("adding chunk of material %d width %d height %d depth %d to (%d %d %d)\n", ck->mat, ck->width, ck->height, ck->depth, ck->x, ck->y, ck->z); //reallocate memory to extend array of chunks in model (reallocate for exactly one instead of growing array for now.. perhaps talk to Devin about improving performance int chunk_size = sizeof(struct chunk); int overlap_case = 0; if (md->nchunks == 0 && ck != NULL) { md->chunks = (struct chunk*) malloc((md->nchunks + 1) * chunk_size);//allocate at least one } else { md->chunks = realloc(md->chunks, (md->nchunks + 1) * chunk_size); } if (md->chunks == NULL) { printf("model_add_chunk: Error allocating more memory!\n"); return 1; } //If we've successfully reallocated, add our new chunk md->chunks[md->nchunks] = *ck; //See if there's an overlapping chunk we must take care of (may have to iterate eventually?) struct chunk previousChunk;//which means this may be better named overlappinChunk struct chunk currentChunk = md->chunks[md->nchunks]; if (md->nchunks > 0) { for (unsigned int i = 0; i < md->nchunks; i++) { previousChunk = md->chunks[i]; //Check overlaps int previousChunkStartingX = previousChunk.x; int previousChunkEndingX = previousChunk.x + previousChunk.width; int currentChunkStartingX = currentChunk.x; int currentChunkEndingX = currentChunk.x + currentChunk.width; overlap_case = hasOverlap(previousChunkStartingX, previousChunkEndingX, currentChunkStartingX, currentChunkEndingX); //previous overlap current on x if (overlap_case == 1) { // printf("@@@@ We have an overlap case with chunk %d!\n", i); break; } } } if (ck->width != 0) { if (overlap_case) { if (ck->x + ck->width > previousChunk.x + previousChunk.width) { if (md->width < ck->x + ck->width) //can only get bigger md->width = ck->x + ck->width; } else { if (md->width < previousChunk.x + previousChunk.width) md->width = previousChunk.x + previousChunk.width; } } else { if (md->nchunks <= 1) md->width = ck->x + ck->width; else if (ck->x + ck->width > md->width) md->width = ck->x + ck->width; } //printf("Model width is now %d\n", md->width); } else { printf("model_add_chunk: Invalid width passed!\n"); free(md->chunks); md->chunks = NULL; return 1; } if (ck->height != 0) { if (overlap_case) { if (ck->y + ck->height > previousChunk.y + previousChunk.height) { if (ck->y + ck->height > md->height)//can only get taller md->height = ck->y + ck->height; } else { if (previousChunk.y + previousChunk.height > md->height)//can only get taller md->height = previousChunk.y + previousChunk.height; } } else md->height = ck->y + ck->height; } else { printf("model_add_chunk: Invalid height passed!\n"); free(md->chunks); md->chunks = NULL; return 1; } //printf("Height is now %d\n", md->height); if (ck->depth != 0) { if (overlap_case) { if (ck->z + ck->depth > previousChunk.z + previousChunk.depth) { if (ck->z + ck->depth > md->depth) md->depth = ck->z + ck->depth; } else { if (previousChunk.z + previousChunk.depth > md->depth) md->depth = previousChunk.z + previousChunk.depth; } } else { if (md->depth < ck->z + ck->depth) md->depth = ck->z + ck->depth;//plus equals or just add? } } else { printf("model_add_chunk: Invalid depth passed!\n"); free(md->chunks); md->chunks = NULL; return 1; } //If we've successfully added a chunk, increase nchunks and see if we need to model model width/depth md->nchunks++; //printf("numberofchunks is now %d\n", md->nchunks); //printf("width is now %d\n", md->width); return 0; }