static void write_stream_reflections(FILE *fh, RefList *list) { Reflection *refl; RefListIterator *iter; fprintf(fh, " h k l I sigma(I) peak background" " fs/px ss/px\n"); for ( refl = first_refl(list, &iter); refl != NULL; refl = next_refl(refl, iter) ) { signed int h, k, l; double intensity, esd_i, bg, pk; double fs, ss; get_indices(refl, &h, &k, &l); get_detector_pos(refl, &fs, &ss); intensity = get_intensity(refl); esd_i = get_esd_intensity(refl); pk = get_peak(refl); bg = get_mean_bg(refl); /* Reflections with redundancy = 0 are not written */ if ( get_redundancy(refl) == 0 ) continue; fprintf(fh, "%4i %4i %4i %10.2f %10.2f %10.2f %10.2f %6.1f %6.1f\n", h, k, l, intensity, esd_i, pk, bg, fs, ss); } }
static void scan_partialities(RefList *reflections, RefList *compare, int *valid, long double *vals[3], int idx, PartialityModel pmodel) { int i; Reflection *refl; RefListIterator *iter; i = 0; for ( refl = first_refl(reflections, &iter); refl != NULL; refl = next_refl(refl, iter) ) { signed int h, k, l; Reflection *refl2; double rlow, rhigh, p; get_indices(refl, &h, &k, &l); refl2 = find_refl(compare, h, k, l); if ( refl2 == NULL ) { valid[i] = 0; i++; continue; } get_partial(refl2, &rlow, &rhigh, &p); vals[idx][i] = p; if ( unlikely(p < 0.0) ) { ERROR("Negative partiality! %3i %3i %3i %f\n", h, k, l, p); } i++; } }
void eval_stress(double *x, double *coord, int *ndim, int *edim, int *nobs, int *samplesize,double *stress) { int i; double dab,simab; int a = 0; int b = 0; long double denom = 0.0; long double numer = 0.0; GetRNGstate(); for (i = 0; i < *samplesize; i++) { get_indices(*nobs, &a, &b); dab = ed(x,a,b,*edim,*nobs); simab = ed(coord,a,b,*ndim,*nobs); denom += simab; numer += (dab - simab) * (dab - simab) / simab; } *stress = (double) (numer/denom); PutRNGstate(); return; }
static void scan_partialities(RefList *reflections, RefList *compare, int *valid, long double *vals[3], int idx) { int i; Reflection *refl; RefListIterator *iter; i = 0; for ( refl = first_refl(reflections, &iter); refl != NULL; refl = next_refl(refl, iter) ) { signed int h, k, l; Reflection *refl2; get_indices(refl, &h, &k, &l); refl2 = find_refl(compare, h, k, l); if ( refl2 == NULL ) { valid[i] = 0; i++; continue; } vals[idx][i] = get_lorentz(refl2); i++; } }
void make_face(char **words, int nwords) { static int warning = 0; int i,ii; Face *f; int vindex; int nindex; int tindex; /* see if we need to allocate space for vertices */ if (max_faces == 0) { max_faces = 200; flist = (Face *) malloc (sizeof (Face) * max_faces); } else if (nfaces == max_faces) { max_faces *= 2; flist = (Face *) realloc (flist, sizeof (Face) * max_faces); } f = &flist[nfaces++]; f->nverts = nwords; f->verts = (int *) malloc (sizeof (int) * nwords); for (i = 0; i < nwords; i++) { get_indices (words[i], &vindex, &tindex, &nindex); #if 0 printf ("vtn: %d %d %d\n", vindex, tindex, nindex); #endif /* maybe flip vertex order */ if (flip_vertex_order) ii = nwords - i - 1; else ii = i; /* store the vertex index */ if (vindex > 0) /* indices are from one, not zero */ f->verts[ii] = vindex - 1; else if (vindex < 0) /* negative indices mean count backwards */ f->verts[ii] = nverts + vindex; else { fprintf (stderr, "Zero indices not allowed: '%s'\n", str_orig); exit (-1); } if ((tindex != 0 || nindex != 0) && warning == 0) { fprintf (stderr, "\n"); fprintf (stderr, "Warning: textures and normals currently ignored.\n"); fprintf (stderr, "\n"); warning = 1; } } }
static int write_stream_reflections_2_2(FILE *fh, RefList *list, struct image *image) { Reflection *refl; RefListIterator *iter; fprintf(fh, " h k l I sigma(I) " "peak background fs/px ss/px\n"); for ( refl = first_refl(list, &iter); refl != NULL; refl = next_refl(refl, iter) ) { signed int h, k, l; double intensity, esd_i, bg, pk; double fs, ss; get_indices(refl, &h, &k, &l); get_detector_pos(refl, &fs, &ss); intensity = get_intensity(refl); esd_i = get_esd_intensity(refl); pk = get_peak(refl); bg = get_mean_bg(refl); /* Reflections with redundancy = 0 are not written */ if ( get_redundancy(refl) == 0 ) continue; if ( image->det != NULL ) { struct panel *p; double write_fs, write_ss; p = find_orig_panel(image->det, fs, ss); if ( p == NULL ) { ERROR("Panel not found\n"); return 1; } /* Convert coordinates to match arrangement of panels in HDF5 * file */ write_fs = fs - p->min_fs + p->orig_min_fs; write_ss = ss - p->min_ss + p->orig_min_ss; fprintf(fh, "%4i %4i %4i %10.2f %10.2f %10.2f %10.2f" " %6.1f %6.1f\n", h, k, l, intensity, esd_i, pk, bg, write_fs, write_ss); } else { fprintf(fh, "%4i %4i %4i %10.2f %10.2f %10.2f %10.2f" " %6.1f %6.1f\n", h, k, l, intensity, esd_i, pk, bg, fs, ss); } } return 0; }
SEXP test_slice_loop(SEXP filenames) { misize_t sizes[3]; double *buffer = NULL; mihandle_t *hvol = NULL; int i, v0, v1, v2; misize_t buffer_index, output_index; int num_files; SEXP output; double *xoutput; /* Rprintf("allocating memory for volumes\n"); num_files = LENGTH(filenames); hvol = malloc(num_files * sizeof(mihandle_t)); Rprintf("Before opening files\n"); open_minc_files(filenames, hvol, sizes); Rprintf("Allocating memory for slice buffer\n"); buffer = malloc(num_files * sizeof(double)); for (i=0; i < num_files; i++) { buffer[i] = malloc(sizes[1] * sizes[2] * sizeof(double)); } */ Rprintf("Before opening files\n"); hvol = open_minc_files(filenames, sizes); Rprintf("Before allocating slice buffer\n"); buffer = create_slice_buffer(filenames, sizes); Rprintf("protecting output\n"); PROTECT(output=allocMatrix(REALSXP, (sizes[0] * sizes[1] * sizes[2]), 1)); xoutput = REAL(output); //create_slice_buffer(filenames, sizes, full_buffer); Rprintf("In slice \n"); for (v0=0; v0 < sizes[0]; v0++) { Rprintf("Before fill_slice_buffer\n"); fill_slice_buffer(filenames, sizes, hvol, buffer, v0); for (v1=0; v1 < sizes[1]; v1++) { for (v2=0; v2 < sizes[2]; v2++) { //Rprintf("Before get_indices\n"); get_indices(v0,v1,v2, sizes, &output_index, &buffer_index); //Rprintf("%ul %ul\n", output_index, buffer_index); xoutput[output_index] = (double) v0; } } } Rprintf("freeing slice buffer\n"); free_slice_buffer(filenames, sizes, buffer); Rprintf("freeing volume handles\n"); free_minc_files(filenames, hvol); Rprintf("finished\n"); UNPROTECT(1); return(output); }
/* FIXME */ void sall_command(client c) { dstr res = get_indices(); dstr *fields = NULL; int nfield = 0, i; RTRIM(res); fields = dstr_split_len(res, dstr_length(res), ",", 1, &nfield); for (i = 1; i < nfield; ++i) { dstr pkey = dstr_new(fields[i]); dstr skey = dstr_new(pkey); dlist_t dlist; struct kvd *kvd; table_rwlock_wrlock(subscribers); if ((dlist = table_get_value(subscribers, pkey)) == NULL) { if (NEW(kvd)) { kvd->key = skey; kvd->u.dlist = dlist_new(NULL, NULL); dlist_insert_tail(kvd->u.dlist, c); dlist = dlist_new(cmpkvd, kdfree); dlist_insert_sort(dlist, kvd); table_insert(subscribers, pkey, dlist); } else { add_reply_error(c, "error allocating memory for kvd\r\n"); dstr_free(skey); dstr_free(pkey); } } else { if (NEW(kvd)) { dlist_node_t node; kvd->key = skey; kvd->u.dlist = dlist_new(NULL, NULL); if ((node = dlist_find(dlist, kvd)) == NULL) { dlist_insert_tail(kvd->u.dlist, c); dlist_insert_sort(dlist, kvd); } else { kdfree(kvd); kvd = (struct kvd *)dlist_node_value(node); if (dlist_find(kvd->u.dlist, c) == NULL) dlist_insert_tail(kvd->u.dlist, c); } } else { add_reply_error(c, "error allocating memory for kvd\r\n"); dstr_free(skey); } dstr_free(pkey); } table_rwlock_unlock(subscribers); } dstr_free(res); //-------------------------------------------------------------------------------------------------------------------- // FIXME //-------------------------------------------------------------------------------------------------------------------- }
std::set<Attribute> Skybox::get_attributes(size_t const size) { std::set<std::tuple<GLenum, OpenGLValue>> values; OpenGLValue points(GL_FLOAT, get_points(size)); OpenGLValue indices(GL_UNSIGNED_INT, get_indices(size)); values.emplace(GL_ARRAY_BUFFER, points); values.emplace(GL_ELEMENT_ARRAY_BUFFER, indices); std::set<Attribute> attribs; attribs.emplace("vpoint", values); return attribs; }
static int write_stream_reflections_2_3(FILE *fh, RefList *list, struct image *image) { Reflection *refl; RefListIterator *iter; fprintf(fh, " h k l I sigma(I) " "peak background fs/px ss/px panel\n"); for ( refl = first_refl(list, &iter); refl != NULL; refl = next_refl(refl, iter) ) { signed int h, k, l; double intensity, esd_i, pk, bg; double fs, ss; double write_fs, write_ss; struct panel *p = NULL; get_indices(refl, &h, &k, &l); get_detector_pos(refl, &fs, &ss); intensity = get_intensity(refl); esd_i = get_esd_intensity(refl); pk = get_peak(refl); bg = get_mean_bg(refl); /* Reflections with redundancy = 0 are not written */ if ( get_redundancy(refl) == 0 ) continue; p = find_panel(image->det,fs,ss); if ( p == NULL ) { ERROR("Panel not found\n"); return 1; } write_fs = fs-p->min_fs+p->orig_min_fs; write_ss = ss-p->min_ss+p->orig_min_ss; fprintf(fh, "%4i %4i %4i %10.2f %10.2f %10.2f %10.2f " "%6.1f %6.1f %s\n", h, k, l, intensity, esd_i, pk, bg, write_fs, write_ss, p->name); } return 0; }
CassError set(StringRef name, const T value) { IndexVec indices; if (get_indices(name, &indices) == 0) { return CASS_ERROR_LIB_NAME_DOES_NOT_EXIST; } for (IndexVec::const_iterator it = indices.begin(), end = indices.end(); it != end; ++it) { size_t index = *it; CassError rc = set(index, value); if (rc != CASS_OK) return rc; } return CASS_OK; }
/* FIXME */ void uall_command(client c) { dstr res = get_indices(); dstr *fields = NULL; int nfield = 0, i; RTRIM(res); fields = dstr_split_len(res, dstr_length(res), ",", 1, &nfield); for (i = 1; i < nfield; ++i) { dstr pkey = dstr_new(fields[i]); dstr skey = dstr_new(pkey); dlist_t dlist; table_rwlock_wrlock(subscribers); if ((dlist = table_get_value(subscribers, pkey))) { struct kvd *kvd; if (NEW(kvd)) { dlist_node_t node, node2; kvd->key = skey; if ((node = dlist_find(dlist, kvd))) { FREE(kvd); kvd = (struct kvd *)dlist_node_value(node); if ((node2 = dlist_find(kvd->u.dlist, c))) dlist_remove(kvd->u.dlist, node2); if (dlist_length(kvd->u.dlist) == 0) { dlist_remove(dlist, node); kdfree(kvd); } if (dlist_length(dlist) == 0) { table_remove(subscribers, pkey); dlist_free(&dlist); } } else FREE(kvd); } else add_reply_error(c, "error allocating memory for kvd"); } table_rwlock_unlock(subscribers); dstr_free(skey); dstr_free(pkey); } add_reply_string(c, "\r\n", 2); dstr_free(res); }
/* テーブルへ追加 */ void add_to_table(DOC *head, char *str) { INDICES *p, *q, *new_ele; unsigned hash_val; hash_val = calc_hash(str); // ハッシュ値の計算 p = hash_table[hash_val].next; q = &hash_table[hash_val]; // 同じ物があるかどうかチェックしながら検索 while (p != NULL) { if (strcmp(p->word, str) == 0) { break; } q = p; p = p->next; } // ヒットしなかったら追加 if (p == NULL) { // 新しく構造体を確保 new_ele = (INDICES *)malloc(sizeof(INDICES)); if (new_ele == NULL) { printf("Memory Allocate Error\n"); exit(1); } // 単語の領域を確保 new_ele->word = (char *)malloc(sizeof(char) * (strlen(str) + 1)); if (new_ele->word == NULL) { printf("Memory Allocate Error\n"); exit(1); } strcpy(new_ele->word, str); // 単語の代入 (new_ele->list).next = get_indices(head, str); // 転置インデックスを作る q->next = new_ele; // 末尾に追加 new_ele->next = NULL; } }
static double guide_dev(Crystal *cr, const RefList *full) { double dev = 0.0; /* For each reflection */ Reflection *refl; RefListIterator *iter; for ( refl = first_refl(crystal_get_reflections(cr), &iter); refl != NULL; refl = next_refl(refl, iter) ) { double G, p; signed int h, k, l; Reflection *full_version; double I_full, I_partial; if ( (get_intensity(refl) < 3.0*get_esd_intensity(refl)) || (get_partiality(refl) < MIN_PART_REFINE) ) continue; get_indices(refl, &h, &k, &l); assert((h!=0) || (k!=0) || (l!=0)); full_version = find_refl(full, h, k, l); if ( full_version == NULL ) continue; /* Some reflections may have recently become scalable, but * scale_intensities() might not yet have been called, so the * full version may not have been calculated yet. */ G = crystal_get_osf(cr); p = get_partiality(refl); I_partial = get_intensity(refl); I_full = get_intensity(full_version); //STATUS("%3i %3i %3i %5.2f %5.2f %5.2f %5.2f %5.2f\n", // h, k, l, G, p, I_partial, I_full, // I_partial - p*G*I_full); dev += pow(I_partial - p*G*I_full, 2.0); } return dev; }
static void write_stream_reflections_2_1(FILE *fh, RefList *list) { Reflection *refl; RefListIterator *iter; fprintf(fh, " h k l I phase sigma(I) " " counts fs/px ss/px\n"); for ( refl = first_refl(list, &iter); refl != NULL; refl = next_refl(refl, iter) ) { signed int h, k, l; double intensity, esd_i, ph; int red; double fs, ss; char phs[16]; int have_phase; get_indices(refl, &h, &k, &l); get_detector_pos(refl, &fs, &ss); intensity = get_intensity(refl); esd_i = get_esd_intensity(refl); red = get_redundancy(refl); ph = get_phase(refl, &have_phase); /* Reflections with redundancy = 0 are not written */ if ( red == 0 ) continue; if ( have_phase ) { snprintf(phs, 16, "%8.2f", rad2deg(ph)); } else { strncpy(phs, " -", 15); } fprintf(fh, "%3i %3i %3i %10.2f %s %10.2f %7i %6.1f %6.1f\n", h, k, l, intensity, phs, esd_i, red, fs, ss); } }
Gadgetron::NFFT_internal::NFFT_Matrix<REAL> Gadgetron::NFFT_internal::make_NFFT_matrix(const Gadgetron::hoNDArray<Gadgetron::vector_td<REAL, D>> trajectories, const Gadgetron::vector_td<size_t, D> &image_dims, REAL W, const Gadgetron::vector_td<REAL, D> &beta) { GadgetronTimer timer("Make NFFT"); // std::vector<std::vector<size_t>> rowind; // rowind.reserve(trajectories.get_number_of_elements() * std::ceil(std::pow(W, 3))); // std::vector<size_t> colptr; // colptr.reserve(trajectories.get_number_of_elements() + 1); // std::vector<std::vector<REAL>> weights; // weights.reserve(trajectories.get_number_of_elements() * std::ceil(std::pow(W, 3))); NFFT_Matrix<REAL> matrix(trajectories.get_number_of_elements(),prod(image_dims)); #pragma omp parallel for for (int i = 0; i < (int)trajectories.get_number_of_elements(); i++) { std::tie(matrix.indices[i], matrix.weights[i]) = get_indices(trajectories[i], image_dims, W, beta); } return matrix; }
void process_file(void *ptr) { char *filename = (char *) ptr; fprintf(stderr,"%s: processing log file %s\n", program_name, filename); FILE *file_to_process = fopen(filename,"r"); if (file_to_process == NULL) { err_sys("Cannot open file %s\n", filename); exit(1); } long file_size = get_size_of_file(file_to_process); // if (is_trivial_file(file_size)) // { // process((void*)file_to_process); // } // else { long num_threads = get_number_of_threads(); long chunk_size = get_chunk_size(file_size, num_threads); pthread_t threads[num_threads]; long byte_endings[num_threads]; long indices[2]; int i; for (i = 0; i < num_threads; ++i) { get_indices(file_to_process, i, chunk_size, byte_endings, indices); // validate_chunk(file_to_process, "validation.txt", indices[0], indices[1], i); process_chunk(file_to_process, i, threads, indices[0], indices[1]); } for (i = 0; i < num_threads; ++i) pthread_join(threads[i], NULL); } }
void sample_distance(double *coord, int *nobs, int *ndim, int *samplesize, double *maxdist) { /* this function will sample the distances between randomly chosen points * and keep track of the maximum distance calculated. This will be used * to get a value of rcut which is a user specified % of the maximum * distance found. (Probability sampling to get rcut is mentioned in * PNAS Dec, 10, 2002, pg 15869 */ double maxd = -1e37; double d = 0; int i; int a = 0; int b = 0; GetRNGstate(); for (i = 0; i < *samplesize; i++) { get_indices(*nobs, &a, &b); d = ed(coord,a,b,*ndim,*nobs); if (d > maxd) maxd = d; } *maxdist = maxd; PutRNGstate(); return; }
void LoadFace(char** strWorld,int nwords){ //int idx[3]; for (int i = 0; i < nwords; i++) { Vertex v={0,0,0,0,0,0,0,0,0}; int vindex=0; int tindex=0; int nindex=0; get_indices (strWorld[i], &vindex, &tindex, &nindex); /* maybe flip vertex order */ if(vindex!=0){ float3& p = vPosition[vindex-1]; v.x = p.x; v.y = p.y; v.z = p.z; v.w = 1.0; } if(tindex!=0){ float2& t = vUV[tindex-1]; v.s = t.x; v.t = t.y; } if(nindex!=0){ float3& n = vNormal[nindex-1]; v.nx = n.x; v.ny = n.y; v.nz = n.z; } vIndex.push_back(AddVertex(v)); } }
void data_store_csv::exchange_data() { double tm1 = get_time(); std::stringstream err; //get indices I need for the next epoch, and start receives std::unordered_set<int> indices; get_indices(indices, m_rank); std::vector<El::mpi::Request<DataType>> recv_req(indices.size()); m_my_minibatch_data.clear(); size_t jj = 0; for (auto data_id : indices) { m_my_minibatch_data[data_id].resize(m_vector_size); int owner = get_index_owner(data_id); if (owner >= m_np or owner < 0) { err << __FILE__ << " " << __LINE__ << " :: " << " ERROR: bad rank for owner in nb_recv; owner: " << owner << " data_id: " << data_id << " jj: " << jj+1 << " of " << indices.size(); throw lbann_exception(err.str()); } m_comm->nb_tagged_recv<DataType>(m_my_minibatch_data[data_id].data(), m_vector_size, owner, data_id, recv_req[jj++], m_comm->get_trainer_comm()); } //start sends to all processors std::vector<std::vector<El::mpi::Request<DataType>>> send_req(m_np); for (int p=0; p<m_np; p++) { get_my_indices(indices, p); send_req[p].resize(indices.size()); jj = 0; for (auto data_id : indices) { if (m_data.find(data_id) == m_data.end()) { err << __FILE__ << " " << __LINE__ << " :: " << " m_data.find(" << data_id << ") failed."; throw lbann_exception(err.str()); } if (m_data[data_id].size() != (size_t)m_vector_size) { err << __FILE__ << " " << __LINE__ << " :: " << " m_data[" << data_id << "].size = " << m_data[data_id].size() << " should be: " << m_vector_size << "; " << jj+1 << " of " << indices.size() << " m_reader->get_role: " << m_reader->get_role(); throw lbann_exception(err.str()); } m_comm->nb_tagged_send<DataType>(m_data[data_id].data(), m_vector_size, p, data_id, send_req[p][jj++], m_comm->get_trainer_comm()); } } //wait for sends to finish if (m_master) { for (size_t i=0; i<send_req.size(); i++) { m_comm->wait_all(send_req[i]); } } //wait for recvs to finish m_comm->wait_all(recv_req); if (m_master) { std::cerr << "TIME for data_store_csv::exchange_data(): " << get_time() - tm1 << "; role: " << m_reader->get_role() << "\n"; } }
ErrorCode ScdInterface::get_shared_vertices(ParallelComm *, ScdBox *, std::vector<int> &, std::vector<int> &, std::vector<int> &) { return MB_FAILURE; #else ErrorCode ScdInterface::get_shared_vertices(ParallelComm *pcomm, ScdBox *box, std::vector<int> &procs, std::vector<int> &offsets, std::vector<int> &shared_indices) { // get index of partitioned dimension const int *ldims = box->box_dims(); ErrorCode rval; int ijkrem[6], ijkface[6], across_bdy[3]; for (int k = -1; k <= 1; k ++) { for (int j = -1; j <= 1; j ++) { for (int i = -1; i <= 1; i ++) { if (!i && !j && !k) continue; int pto; int dijk[] = {i, j, k}; rval = get_neighbor(pcomm->proc_config().proc_size(), pcomm->proc_config().proc_rank(), box->par_data(), dijk, pto, ijkrem, ijkface, across_bdy); if (MB_SUCCESS != rval) return rval; if (-1 != pto) { if (procs.empty() || pto != *procs.rbegin()) { procs.push_back(pto); offsets.push_back(shared_indices.size()); } rval = get_indices(ldims, ijkrem, across_bdy, ijkface, shared_indices); if (MB_SUCCESS != rval) return rval; // check indices against known #verts on local and remote // begin of this block is shared_indices[*offsets.rbegin()], end is shared_indices.end(), halfway // is (shared_indices.size()-*offsets.rbegin())/2 #ifndef NDEBUG int start_idx = *offsets.rbegin(), end_idx = shared_indices.size(), mid_idx = (start_idx+end_idx)/2; int num_local_verts = (ldims[3]-ldims[0]+1)*(ldims[4]-ldims[1]+1)* (-1 == ldims[2] && -1 == ldims[5] ? 1 : (ldims[5]-ldims[2]+1)), num_remote_verts = (ijkrem[3]-ijkrem[0]+1)*(ijkrem[4]-ijkrem[1]+1)* (-1 == ijkrem[2] && -1 == ijkrem[5] ? 1 : (ijkrem[5]-ijkrem[2]+1)); assert(*std::min_element(&shared_indices[start_idx], &shared_indices[mid_idx]) >= 0 && *std::max_element(&shared_indices[start_idx], &shared_indices[mid_idx]) < num_local_verts && *std::min_element(&shared_indices[mid_idx], &shared_indices[end_idx]) >= 0 && *std::max_element(&shared_indices[mid_idx], &shared_indices[end_idx]) < num_remote_verts); #endif } } } } offsets.push_back(shared_indices.size()); return MB_SUCCESS; #endif } } // namespace moab
static double test_gradients(Crystal *cr, double incr_val, int refine, const char *str, const char *file, PartialityModel pmodel, int quiet, int plot) { Reflection *refl; RefListIterator *iter; long double *vals[3]; int i; int *valid; int nref; int n_good, n_invalid, n_small, n_nan, n_bad; RefList *reflections; FILE *fh = NULL; int ntot = 0; double total = 0.0; char tmp[32]; double *vec1; double *vec2; int n_line; double cc; reflections = find_intersections(crystal_get_image(cr), cr, pmodel); crystal_set_reflections(cr, reflections); nref = num_reflections(reflections); if ( nref < 10 ) { ERROR("Too few reflections found. Failing test by default.\n"); return 0.0; } vals[0] = malloc(nref*sizeof(long double)); vals[1] = malloc(nref*sizeof(long double)); vals[2] = malloc(nref*sizeof(long double)); if ( (vals[0] == NULL) || (vals[1] == NULL) || (vals[2] == NULL) ) { ERROR("Couldn't allocate memory.\n"); return 0.0; } valid = malloc(nref*sizeof(int)); if ( valid == NULL ) { ERROR("Couldn't allocate memory.\n"); return 0.0; } for ( i=0; i<nref; i++ ) valid[i] = 1; scan_partialities(reflections, reflections, valid, vals, 1, pmodel); calc_either_side(cr, incr_val, valid, vals, refine, pmodel); if ( plot ) { snprintf(tmp, 32, "gradient-test-%s.dat", file); fh = fopen(tmp, "w"); } vec1 = malloc(nref*sizeof(double)); vec2 = malloc(nref*sizeof(double)); if ( (vec1 == NULL) || (vec2 == NULL) ) { ERROR("Couldn't allocate memory.\n"); return 0.0; } n_invalid = 0; n_good = 0; n_nan = 0; n_small = 0; n_bad = 0; n_line = 0; i = 0; for ( refl = first_refl(reflections, &iter); refl != NULL; refl = next_refl(refl, iter) ) { long double grad1, grad2, grad; double cgrad; signed int h, k, l; get_indices(refl, &h, &k, &l); if ( !valid[i] ) { n_invalid++; i++; } else { double r1, r2, p; grad1 = (vals[1][i] - vals[0][i]) / incr_val; grad2 = (vals[2][i] - vals[1][i]) / incr_val; grad = (grad1 + grad2) / 2.0; i++; cgrad = p_gradient(cr, refine, refl, pmodel); get_partial(refl, &r1, &r2, &p); if ( isnan(cgrad) ) { n_nan++; continue; } if ( plot ) { fprintf(fh, "%e %Le\n", cgrad, grad); } vec1[n_line] = cgrad; vec2[n_line] = grad; n_line++; if ( (fabs(cgrad) < 5e-8) && (fabs(grad) < 5e-8) ) { n_small++; continue; } total += fabs(cgrad - grad); ntot++; if ( !within_tolerance(grad, cgrad, 5.0) || !within_tolerance(cgrad, grad, 5.0) ) { if ( !quiet ) { STATUS("!- %s %3i %3i %3i" " %10.2Le %10.2e ratio = %5.2Lf" " %10.2e %10.2e\n", str, h, k, l, grad, cgrad, cgrad/grad, r1, r2); } n_bad++; } else { //STATUS("OK %s %3i %3i %3i" // " %10.2Le %10.2e ratio = %5.2Lf" // " %10.2e %10.2e\n", // str, h, k, l, grad, cgrad, cgrad/grad, // r1, r2); n_good++; } } } STATUS("%3s: %3i within 5%%, %3i outside, %3i nan, %3i invalid, " "%3i small. ", str, n_good, n_bad, n_nan, n_invalid, n_small); if ( plot ) { fclose(fh); } cc = gsl_stats_correlation(vec1, 1, vec2, 1, n_line); STATUS("CC = %+f\n", cc); return cc; }
SEXP per_voxel_anova(SEXP filenames, SEXP Sx, SEXP asgn, SEXP have_mask, SEXP mask) { /* generic items for all slice_loop functions */ misize_t sizes[3]; double **full_buffer; double *mask_buffer; mihandle_t *hvol, hmask; double *use_mask; int v0, v1, v2; int num_files, i; misize_t buffer_index, output_index; /* anova specific items */ int n,p, maxasgn; int *xasgn; double *coefficients, *residuals, *effects, *work, *qraux, *v, *diag, *se, *t, *comp, *ss; int *pivot, *df; SEXP Soutput, t_sexp, Sdata; double *output, *data; use_mask = REAL(have_mask); //Rprintf("Use mask: %f\n", use_mask[0]); num_files = LENGTH(filenames); //Rprintf("Before opening files\n"); hvol = open_minc_files(filenames, sizes); //Rprintf("Before getting mask\n"); if (use_mask[0] == 1) { Rprintf("Getting mask\n"); //get_mask(mask, hmask, mask_buffer, sizes); miopen_volume(CHAR(STRING_ELT(mask, 0)), MI2_OPEN_READ, &hmask); mask_buffer = malloc(sizes[1]*sizes[2]*sizeof(double)); } //Rprintf("Before creating slice buffer\n"); //full_buffer = malloc(num_files * sizeof(double)); full_buffer = create_slice_buffer(filenames, sizes); //num_files = LENGTH(filenames); //full_buffer = malloc(num_files * sizeof(double)); //for (i=0; i < num_files; i++) { // full_buffer[i] = malloc(sizes[1] * sizes[2] * sizeof(double)); //} /* ANOVA specific allocations */ n = nrows(Sx); p = ncols(Sx); xasgn = INTEGER(asgn); maxasgn = 0; for (i=0; i < p; i++) { if (xasgn[i] > maxasgn) { maxasgn = (int) xasgn[i]; } } maxasgn++; coefficients = malloc(sizeof(double) * p); residuals = malloc(sizeof(double) * n); effects = malloc(sizeof(double) * n); pivot = malloc(sizeof(int) * p); work = malloc(sizeof(double) * (2*p)); qraux = malloc(sizeof(double) * p); v = malloc(sizeof(double) * p * p); diag = malloc(sizeof(double) * p); se = malloc(sizeof(double) * p); t = malloc(sizeof(double) * p); comp = malloc(sizeof(double) * p); ss = malloc(sizeof(double) * maxasgn); df = malloc(sizeof(int) * maxasgn); Rprintf("N: %d P: %d\n", n,p); PROTECT(t_sexp = allocVector(REALSXP, maxasgn-1)); //Rprintf("Sizes: %d %d %d\n", sizes[0], sizes[1], sizes[2]); /* allocate the output buffer */ PROTECT(Soutput=allocMatrix(REALSXP, (sizes[0] * sizes[1] * sizes[2]), maxasgn-1)); output = REAL(Soutput); /* allocate the local buffer that will be passed to the function */ PROTECT(Sdata = allocVector(REALSXP, LENGTH(filenames))); data = REAL(Sdata); /* enter slice loop */ Rprintf("In slice \n"); for (v0=0; v0 < sizes[0]; v0++) { //Rprintf("Before fill_slice_buffer\n"); fill_slice_buffer(filenames, sizes, hvol, full_buffer, v0); //Rprintf("After fill_slice_buffer\n"); if (use_mask[0] == 1) { get_mask_slice(hmask, sizes, mask_buffer, v0); } for (v1=0; v1 < sizes[1]; v1++) { for (v2=0; v2 < sizes[2]; v2++) { //Rprintf("Before get_indices\n"); get_indices(v0,v1,v2, sizes, &output_index, &buffer_index); /* only perform operation if not masked */ if (use_mask[0] == 0 || (use_mask[0] == 1 && mask_buffer[buffer_index] > 0.5)) { /* fill data buffer */ for (i=0; i< num_files; i++) { data[i] = full_buffer[i][buffer_index]; } /* compute the linear model */ //Rprintf("Before voxel_anova\n"); t_sexp = voxel_anova(Sdata, Sx, asgn, coefficients, residuals, effects, work, qraux, v, pivot, se, t, comp, ss, df); //Rprintf("Before assigning of output\n"); for (i=0; i < maxasgn-1; i++) { output[output_index + i * (sizes[0]*sizes[1]*sizes[2])] = REAL(t_sexp)[i]; } } else { for (i=0; i < maxasgn-1; i++) { output[output_index + i * (sizes[0]*sizes[1]*sizes[2])] = 0.0; } } } } } //Rprintf("freeing slice buffer\n"); free_slice_buffer(filenames, sizes, full_buffer); //Rprintf("freeing volume handles\n"); free_minc_files(filenames, hvol); //Rprintf("finished\n"); free(coefficients); free(residuals); free(effects); free(pivot); free(work); free(qraux); free(v); free(diag); free(se); free(t); free(comp); free(ss); free(df); UNPROTECT(3); Rprintf("\nDone\n"); return(Soutput); }
void make_face ( char **words, int nwords ) /******************************************************************************/ /* Purpose: MAKE_FACE creates a new face. Modified: 18 January 2011 Author: Greg Turk Entry: Input, char **WORDS, a list of words describing the vertex. Input, int NWORDS, the number of words in the list. */ { int i; int ii; Face *f; int nindex; int tindex; int vindex; static int warning = 0; /* See if we need to allocate space for vertices. */ if ( max_faces == 0 ) { max_faces = 200; flist = ( Face * ) malloc ( sizeof ( Face ) * max_faces ); } else if ( max_faces == nfaces ) { max_faces = max_faces * 2; flist = ( Face * ) realloc ( flist, sizeof ( Face ) * max_faces ); } f = &flist[nfaces++]; f->nverts = nwords; f->verts = (int *) malloc ( sizeof ( int ) * nwords ); for ( i = 0; i < nwords; i++ ) { get_indices (words[i], &vindex, &tindex, &nindex ); /* Maybe flip vertex order. */ if ( flip_vertex_order ) { ii = nwords - i - 1; } else { ii = i; } /* Store the vertex index. */ /* Indices seem to start at 1, not zero? */ if ( 0 < vindex ) { f->verts[ii] = vindex - 1; } /* Indices are negative, so counting backwards? */ else if (vindex < 0) { f->verts[ii] = nverts + vindex; } else { fprintf (stderr, "Zero indices not allowed: '%s'\n", str_orig); exit (-1); } if ((tindex != 0 || nindex != 0) && warning == 0) { fprintf (stderr, "\n"); fprintf (stderr, "Warning: textures and normals currently ignored.\n"); fprintf (stderr, "\n"); warning = 1; } } return; }
void TypeRegistry::define_properties() { for (TypeMap::iterator j=tfmap_.begin(); j!=tfmap_.end(); ++j) { for (TypeList::iterator i=j->second.begin(); i!=j->second.end(); ++i) { typedef std::map<std::string, FunctionList> NameFunctionMap; typedef std::map<std::string, NameFunctionMap> FunctionMap; typedef std::map<std::size_t, FunctionMap> IndexFunctionMap; typedef std::map<std::string, PropertyList> PropertyMap; PropertyMap newprops; NameFunctionMap count_candidates; FunctionMap add_candidates; FunctionMap insert_candidates; IndexFunctionMap remove_candidates; IndexFunctionMap get_candidates; IndexFunctionMap set_candidates; typedef std::map<std::string, StringSet> NameTypeMap; NameTypeMap nameTypeMap; std::size_t max_indices = 0; for (FunctionList::const_iterator k=i->methods.begin(); k!=i->methods.end(); ++k) { if (!k->is_constructor(*i) && !k->is_destructor() && !k->is_static) { if (begins_with(k->name, "getOr") && k->name.size() > 5 && std::isupper(k->name[5], std::locale())) continue; if (begins_with(k->name, "getNum") && k->name.size() > 6 && std::isupper(k->name[6], std::locale())) { if (k->params.empty()) { std::string name(k->name.substr(6)); count_candidates[name].push_back(*k); continue; } } if (begins_with(k->name, "get") && ((k->name.size() > 3 && std::isupper(k->name[3], std::locale())) || (k->name.size() == 3))) { std::string name(k->name.substr(3)); std::size_t indices = k->params.size(); get_candidates[indices][k->return_type_specifier][name].push_back(*k); if (indices > max_indices) max_indices = indices; nameTypeMap[name].insert(k->return_type_specifier); continue; } if (begins_with(k->name, "set") && k->name.size() > 3 && std::isupper(k->name[3], std::locale())) { if (!k->params.empty()) { std::string name(k->name.substr(3)); std::size_t indices = k->params.size() - 1; set_candidates[indices][k->params.back().type_specifier][name].push_back(*k); if (indices > max_indices) max_indices = indices; nameTypeMap[name].insert(k->params.back().type_specifier); } continue; } if (begins_with(k->name, "add") && k->name.size() > 3 && std::isupper(k->name[3], std::locale())) { if (k->params.size() == 1) { std::string name(k->name.substr(3)); add_candidates[k->params.front().type_specifier][name].push_back(*k); nameTypeMap[name].insert(k->params.front().type_specifier); } continue; } if (begins_with(k->name, "remove") && k->name.size() > 6 && std::isupper(k->name[6], std::locale())) { if (!k->params.empty()) { std::string name(k->name.substr(6)); std::size_t indices = k->params.size(); remove_candidates[indices][k->params.front().type_specifier][name].push_back(*k); } continue; } if (begins_with(k->name, "insert") && k->name.size() > 6 && std::isupper(k->name[6], std::locale())) { if (k->params.size() >= 2) { std::string name(k->name.substr(6)); insert_candidates[k->params.front().type_specifier][name].push_back(*k); } continue; } } } for (NameTypeMap::const_iterator k=nameTypeMap.begin(); k!=nameTypeMap.end(); ++k) { StringSet::const_iterator endIt = k->second.end(); for (StringSet::const_iterator h=k->second.begin(); h!=endIt; ++h) { PropertyDesc pd; pd.name = k->first; pd.type_name = *h; // simple property { const FunctionList &fl_get = get_candidates[0][*h][k->first]; FunctionList fl_set; fl_set.insert(fl_set.end(), set_candidates[0][*h][k->first].begin(), set_candidates[0][*h][k->first].end()); fl_set.insert(fl_set.end(), set_candidates[0]["const " + *h + " &"][k->first].begin(), set_candidates[0]["const " + *h + " &"][k->first].end()); if (!fl_get.empty()) pd.get_method = fl_get.front().name_signature; if (!fl_set.empty()) pd.set_method = fl_set.front().name_signature; if (!pd.get_method.empty() || !pd.set_method.empty()) { pd.type = PropertyDesc::SIMPLE; newprops[pd.name].push_back(pd); continue; } } // array property { FunctionList fl_count; fl_count.insert(fl_count.end(), count_candidates[k->first].begin(), count_candidates[k->first].end()); fl_count.insert(fl_count.end(), count_candidates[k->first + "s"].begin(), count_candidates[k->first + "s"].end()); fl_count.insert(fl_count.end(), count_candidates[k->first + "es"].begin(), count_candidates[k->first + "es"].end()); fl_count.insert(fl_count.end(), count_candidates[k->first + "ren"].begin(), count_candidates[k->first + "ren"].end()); if (fl_count.size()) { std::string& return_type_specifier = fl_count.front().return_type_specifier; const FunctionList &fl_get = get_candidates[1][*h][k->first]; FunctionList fl_set; fl_set.insert(fl_set.end(), set_candidates[1][*h][k->first].begin(), set_candidates[1][*h][k->first].end()); fl_set.insert(fl_set.end(), set_candidates[1]["const " + *h + " &"][k->first].begin(), set_candidates[1]["const " + *h + " &"][k->first].end()); const FunctionList &fl_add = add_candidates[*h][k->first]; FunctionList fl_remove; fl_remove.insert(fl_remove.end(), remove_candidates[1][return_type_specifier][k->first].begin(), remove_candidates[1][return_type_specifier][k->first].end()); fl_remove.insert(fl_remove.end(), remove_candidates[1][return_type_specifier][k->first + "s"].begin(), remove_candidates[1][return_type_specifier][k->first + "s"].end()); fl_remove.insert(fl_remove.end(), remove_candidates[1][return_type_specifier][k->first + "es"].begin(), remove_candidates[1][return_type_specifier][k->first + "es"].end()); fl_remove.insert(fl_remove.end(), remove_candidates[1][return_type_specifier][k->first + "ren"].begin(), remove_candidates[1][return_type_specifier][k->first + "ren"].end()); fl_remove.insert(fl_remove.end(), remove_candidates[2][return_type_specifier][k->first].begin(), remove_candidates[2][return_type_specifier][k->first].end()); fl_remove.insert(fl_remove.end(), remove_candidates[2][return_type_specifier][k->first + "s"].begin(), remove_candidates[2][return_type_specifier][k->first + "s"].end()); fl_remove.insert(fl_remove.end(), remove_candidates[2][return_type_specifier][k->first + "es"].begin(), remove_candidates[2][return_type_specifier][k->first + "es"].end()); fl_remove.insert(fl_remove.end(), remove_candidates[2][return_type_specifier][k->first + "ren"].begin(), remove_candidates[2][return_type_specifier][k->first + "ren"].end()); FunctionList fl_insert = insert_candidates[return_type_specifier][k->first]; fl_insert.erase(std::remove_if(fl_insert.begin(), fl_insert.end(), PropertyFunctionFirstParameterNameFilter<2>(fl_count.front().return_type_specifier)), fl_insert.end()); fl_remove.erase(std::remove_if(fl_remove.begin(), fl_remove.end(), PropertyFunctionFirstParameterNameFilter<1>(fl_count.front().return_type_specifier)), fl_remove.end()); if (!fl_get.empty()) pd.get_method = fl_get.front().name_signature; if (!fl_set.empty()) pd.set_method = fl_set.front().name_signature; if (!fl_add.empty()) pd.add_method = fl_add.front().name_signature; if (!fl_insert.empty()) pd.insert_method = fl_insert.front().name_signature; if (!fl_remove.empty()) pd.remove_method = fl_remove.front().name_signature; if (!fl_count.empty()) pd.count_method = fl_count.front().name_signature; if (!pd.get_method.empty() || !pd.set_method.empty()) { pd.type = PropertyDesc::ARRAY; newprops[pd.name].push_back(pd); continue; } } } // indexed property for (std::size_t u=1; u<=max_indices; ++u) { const FunctionList &fl_get = get_candidates[u][*h][k->first]; FunctionList fl_set; fl_set.insert(fl_set.end(), set_candidates[u][*h][k->first].begin(), set_candidates[u][*h][k->first].end()); fl_set.insert(fl_set.end(), set_candidates[u]["const " + *h + " &"][k->first].begin(), set_candidates[u]["const " + *h + " &"][k->first].end()); if (!fl_get.empty()) pd.get_method = fl_get.front().name_signature; if (!fl_set.empty()) pd.set_method = fl_set.front().name_signature; for (FunctionList::const_iterator x=fl_get.begin(); x!=fl_get.end(); ++x) { ParameterList get_indices(x->params.begin(), x->params.end()); for (FunctionList::const_iterator y=fl_set.begin(); y!=fl_set.end(); ++y) { ParameterList set_indices(y->params.begin(), y->params.end()); set_indices.pop_back(); if (same_parameters(get_indices, set_indices)) { pd.type = PropertyDesc::INDEXED; pd.get_method = x->name_signature; pd.set_method = y->name_signature; newprops[pd.name].push_back(pd); break; } } } } } } for (PropertyMap::iterator k=newprops.begin(); k!=newprops.end(); ++k) { if (!k->second.empty()) { std::sort(k->second.begin(), k->second.end(), PropertySorter()); PropertyDesc & pd = k->second.front(); const PropertyOptions *opt = cfg_.getPropertyOptions(i->type_name, pd.name); if (opt) { if (!opt->get_method.empty()) pd.get_method = opt->get_method; if (!opt->set_method.empty()) pd.set_method = opt->set_method; if (!opt->remove_method.empty()) pd.remove_method = opt->remove_method; if (!opt->add_method.empty()) pd.add_method = opt->add_method; if (!opt->insert_method.empty()) pd.insert_method = opt->insert_method; if (!opt->count_method.empty()) { Notify::info("define count method of property `" + pd.name + "' in type `" + i->type_name + "' on user request"); pd.type = PropertyDesc::ARRAY; pd.count_method = opt->count_method; } } i->properties.push_back(k->second.front()); } } } } }
void operator()(std::tuple<Ts...>& xs) { apply_args(*this, get_indices(xs), xs); }
void consume(std::tuple<Ts...>& x) { result_ += '('; apply_args(*this, get_indices(x), x); result_ += ')'; }
static struct flist *asymm_and_merge(RefList *in, const SymOpList *sym, UnitCell *cell, double rmin, double rmax, SymOpList *amb) { Reflection *refl; RefListIterator *iter; RefList *asym; struct flist *f; int n; asym = reflist_new(); if ( asym == NULL ) return NULL; for ( refl = first_refl(in, &iter); refl != NULL; refl = next_refl(refl, iter) ) { signed int h, k, l; signed int ha, ka, la; Reflection *cr; double res; get_indices(refl, &h, &k, &l); if ( cell == NULL ) { ERROR("Can't calculate resolution cutoff - no cell\n"); } else { res = 2.0*resolution(cell, h, k, l); if ( res < rmin ) continue; if ( res > rmax ) continue; } get_asymm(sym, h, k, l, &ha, &ka, &la); if ( amb != NULL ) { signed int hr, kr, lr; signed int hra, kra, lra; get_equiv(amb, NULL, 0, ha, ka, la, &hr, &kr, &lr); get_asymm(sym, hr, kr, lr, &hra, &kra, &lra); /* Skip twin-proof reflections */ if ( (ha==hra) && (ka==kra) && (la==lra) ) { //STATUS("%i %i %i is twin proof\n", h, k, l); continue; } } cr = find_refl(asym, ha, ka, la); if ( cr == NULL ) { cr = add_refl(asym, ha, ka, la); assert(cr != NULL); copy_data(cr, refl); } else { const double i = get_intensity(cr); const int r = get_redundancy(cr); set_intensity(cr, (r*i + get_intensity(refl))/(r+1)); set_redundancy(cr, r+1); } } f = malloc(sizeof(struct flist)); if ( f == NULL ) { ERROR("Failed to allocate flist\n"); return NULL; } n = num_reflections(asym); f->s = malloc(n*sizeof(unsigned int)); f->s_reidx = malloc(n*sizeof(unsigned int)); f->i = malloc(n*sizeof(float)); f->i_reidx = malloc(n*sizeof(float)); if ( (f->s == NULL) || (f->i == NULL) || (f->s_reidx == NULL) || (f->i_reidx == NULL) ) { ERROR("Failed to allocate flist\n"); return NULL; } f->n = 0; for ( refl = first_refl(asym, &iter); refl != NULL; refl = next_refl(refl, iter) ) { signed int h, k, l; get_indices(refl, &h, &k, &l); f->s[f->n] = SERIAL(h, k, l); f->i[f->n] = get_intensity(refl); f->n++; } assert(f->n == n); if ( amb != NULL ) { RefList *reidx = reflist_new(); if ( reidx == NULL ) return NULL; for ( refl = first_refl(asym, &iter); refl != NULL; refl = next_refl(refl, iter) ) { signed int h, k, l; signed int hr, kr, lr; signed int hra, kra, lra; Reflection *cr; get_indices(refl, &h, &k, &l); get_equiv(amb, NULL, 0, h, k, l, &hr, &kr, &lr); get_asymm(sym, hr, kr, lr, &hra, &kra, &lra); cr = add_refl(reidx, hra, kra, lra); copy_data(cr, refl); } n = 0; for ( refl = first_refl(reidx, &iter); refl != NULL; refl = next_refl(refl, iter) ) { signed int h, k, l; get_indices(refl, &h, &k, &l); f->s_reidx[n] = SERIAL(h, k, l); f->i_reidx[n++] = get_intensity(refl); } reflist_free(reidx); } reflist_free(asym); return f; }
slice::range<RandomAccessIterator> get_indicies( const RandomAccessIterator& begin, const RandomAccessIterator& end) const { return get_indices(begin, end); }
void ObjReader::readObj(char* fileName, int &nVertices, float **vertices, float **normals, float **texcoords, int &nIndices, int **indices) { FILE * fp = fopen(fileName,"r"); int nv=0, nn=0, nf=0, nt=0; char line[81]; while ( fgets( line, 80, fp ) != NULL ) { if (line[0] == 'v') { if (line[1] == 'n') nn++; else if (line[1] == 't') nt++; else nv++; } else if (line[0] == 'f') { nf++; } } fseek(fp, 0, 0); // Arrays for normals, vertices, texture coords float *n = new float[3*(nn>nf?nn:nf)]; float *v = new float[3*nv]; float *t = new float[2*nt]; // Index arrays for normals, vertices, texture coords int *vInd = new int[3*nf]; int *nInd = new int[3*nf]; int *tInd = new int[3*nf]; int nvertices = 0; int nnormals = 0; int ntexcoords = 0; int nindices = 0; int ntriangles = 0; bool noNormals = false; bool noTexCoords = false; while ( fgets( line, 80, fp ) != NULL ) { if (line[0] == 'v') { if (line[1] == 'n') { float x, y, z; sscanf( &line[2], "%f %f %f\n", &x, &y, &z); float l = sqrt(x*x+y*y+z*z); x = x/l; y = y/l; z = z/l; n[nnormals]=x; nnormals++; n[nnormals]=y; nnormals++; n[nnormals]=z; nnormals++; } else if (line[1] == 't') { float u, v; sscanf( &line[2], "%f %f\n", &u, &v); t[ntexcoords]=u; ntexcoords++; t[ntexcoords]=v; ntexcoords++; } else { float x, y, z; sscanf( &line[1], "%f %f %f\n", &x, &y, &z); v[nvertices] = x; nvertices++; v[nvertices] = y; nvertices++; v[nvertices] = z; nvertices++; } } else if (line[0] == 'f') { char s1[32], s2[32], s3[32]; int vI, tI, nI; sscanf( &line[1], "%s %s %s\n", s1, s2, s3); // Indices for first vertex get_indices(s1, &vI, &tI, &nI); vInd[nindices] = vI-1; if (nI) nInd[nindices] = nI-1; else noNormals = true; if (tI) tInd[nindices] = tI-1; else noTexCoords = true; nindices++; // Indices for second vertex get_indices(s2, &vI, &tI, &nI); vInd[nindices] = vI-1; if (nI) nInd[nindices] = nI-1; else noNormals = true; if (tI) tInd[nindices] = tI-1; else noTexCoords = true; nindices++; // Indices for third vertex get_indices(s3, &vI, &tI, &nI); vInd[nindices] = vI-1; if (nI) nInd[nindices] = nI-1; else noNormals = true; if (tI) tInd[nindices] = tI-1; else noTexCoords = true; nindices++; ntriangles++; } } /* We don't support separate indices for normals, vertices, and texture coordinates. Need to re-organize arrays. */ *vertices = new float[ntriangles*9]; if(!noNormals) *normals = new float[ntriangles*9]; else *normals=0; if(!noTexCoords) *texcoords = new float[ntriangles*6]; else *texcoords=0; *indices = new int[ntriangles*3]; nVertices = ntriangles*3; nIndices = ntriangles*3; for(int i=0; i<ntriangles; i++) { (*indices)[3*i] = 3*i; (*indices)[3*i+1] = 3*i+1; (*indices)[3*i+2] = 3*i+2; (*vertices)[9*i] = v[3*vInd[3*i]]; (*vertices)[9*i+1] = v[3*vInd[3*i]+1]; (*vertices)[9*i+2] = v[3*vInd[3*i]+2]; (*vertices)[9*i+3] = v[3*vInd[3*i+1]]; (*vertices)[9*i+4] = v[3*vInd[3*i+1]+1]; (*vertices)[9*i+5] = v[3*vInd[3*i+1]+2]; (*vertices)[9*i+6] = v[3*vInd[3*i+2]]; (*vertices)[9*i+7] = v[3*vInd[3*i+2]+1]; (*vertices)[9*i+8] = v[3*vInd[3*i+2]+2]; if(!noNormals) { (*normals)[9*i] = n[3*nInd[3*i]]; (*normals)[9*i+1] = n[3*nInd[3*i]+1]; (*normals)[9*i+2] = n[3*nInd[3*i]+2]; (*normals)[9*i+3] = n[3*nInd[3*i+1]]; (*normals)[9*i+4] = n[3*nInd[3*i+1]+1]; (*normals)[9*i+5] = n[3*nInd[3*i+1]+2]; (*normals)[9*i+6] = n[3*nInd[3*i+2]]; (*normals)[9*i+7] = n[3*nInd[3*i+2]+1]; (*normals)[9*i+8] = n[3*nInd[3*i+2]+2]; } if(!noTexCoords) { (*texcoords)[6*i] = t[2*tInd[3*i]]; (*texcoords)[6*i+1] = t[2*tInd[3*i]+1]; (*texcoords)[6*i+2] = t[2*tInd[3*i+1]]; (*texcoords)[6*i+3] = t[2*tInd[3*i+1]+1]; (*texcoords)[6*i+4] = t[2*tInd[3*i+2]]; (*texcoords)[6*i+5] = t[2*tInd[3*i+2]+1]; } } delete[] n; delete[] v; delete[] t; delete[] nInd; delete[] vInd; delete[] tInd; }