Ref<Shape> Mesh::create_trimesh_shape() const { DVector<Face3> faces = get_faces(); if (faces.size()==0) return Ref<Shape>(); DVector<Vector3> face_points; face_points.resize( faces.size()*3 ); for (int i=0;i<face_points.size();i++) { Face3 f = faces.get( i/3 ); face_points.set(i, f.vertex[i%3] ); } Ref<ConcavePolygonShape> shape = memnew( ConcavePolygonShape ); shape->set_faces(face_points); return shape; }
DVector<Vector2> TileMapEditor::_bucket_fill(const Point2i& p_start) { if (node->get_cell(p_start.x, p_start.y) != TileMap::INVALID_CELL) return DVector<Vector2>(); int id = get_selected_tile(); if (id == TileMap::INVALID_CELL) return DVector<Vector2>(); Rect2 r = node->get_item_rect(); r.pos = r.pos/node->get_cell_size(); r.size = r.size/node->get_cell_size(); DVector<Vector2> points; List<Point2i> queue; queue.push_back(p_start); while (queue.size()) { Point2i n = queue.front()->get(); queue.pop_front(); if (!r.has_point(n)) continue; if (node->get_cell(n.x, n.y) == TileMap::INVALID_CELL) { node->set_cellv(n, id, flip_h, flip_v, transpose); points.push_back(n); queue.push_back(n + Point2i(0, 1)); queue.push_back(n + Point2i(0, -1)); queue.push_back(n + Point2i(1, 0)); queue.push_back(n + Point2i(-1, 0)); } } return points; }
/*** * Manage UDP packets */ void NetGameServer::_handle_udp() { DVector<uint8_t> raw; NetGameServerConnection *cd; // Flush packets queue while(udp_queue.size() > 0) { // Only this thread removes from the queue // it is safe to lock here udp_mutex->lock(); QueuedPacket *qp = udp_queue.get(0); udp_queue.remove(0); udp_mutex->unlock(); NetGameServerConnection *cd = _get_client(qp->id); if(cd != NULL) { udp_server->set_send_address(cd->udp_host, cd->udp_port); udp_server->put_packet_buffer(cd->build_pkt(qp)); } memdelete(qp); } // Handle incoming packets if(udp_server->get_available_packet_count() > 0) { udp_server->get_packet_buffer(raw); if(raw.size() < 1) { WARN_PRINT("Invalid UDP Packet!"); return; } cd = _get_client(raw.get(0)); if(cd == NULL) { WARN_PRINT("Invalid UDP Auth!"); return; } cd->handle_udp(raw, udp_server->get_packet_address(), udp_server->get_packet_port()); } }
/* >>> start tutorial code >>> */ int main( ){ USING_NAMESPACE_ACADO // DEFINE VALRIABLES: // --------------------------- DMatrix A(3,3); DVector b(3) ; DifferentialState x("", 3, 1); Function f ; // DEFINE THE VECTOR AND MATRIX ENTRIES: // ------------------------------------- A.setZero() ; A(0,0) = 1.0; A(1,1) = 2.0; A(2,2) = 3.0; b(0) = 1.0; b(1) = 1.0; b(2) = 1.0; // DEFINE A TEST FUNCTION: // ----------------------- f << A*x + b; f << ( A*x(0) ).getRow( 1 ); f << ( A*x(0) ).getCol( 0 ); // TEST THE FUNCTION f: // -------------------- EvaluationPoint zz(f); DVector xx(3); xx(0) = 1.0; xx(1) = 2.0; xx(2) = 3.0; zz.setX( xx ); DVector result = f.evaluate( zz ); result.print(std::cout, "result"); return 0; }
void SampleManagerMallocSW::sample_set_data(RID p_sample, const DVector<uint8_t>& p_buffer) { Sample *s = sample_owner.get(p_sample); ERR_FAIL_COND(!s); int buff_size=p_buffer.size(); ERR_FAIL_COND(buff_size==0); ERR_EXPLAIN("Sample buffer size does not match sample size."); ERR_FAIL_COND(s->length_bytes!=buff_size); DVector<uint8_t>::Read buffer_r=p_buffer.read(); const uint8_t *src = buffer_r.ptr(); uint8_t *dst = (uint8_t*)s->data; for(int i=0;i<s->length_bytes;i++) { dst[i]=src[i]; } }
returnValue Integrator::diffTransitionBackward( DVector &DX, DVector &DP, DVector &DU, DVector &DW, int &order ){ ASSERT( transition != 0 ); if( order != 1 ) return ACADOERROR( RET_NOT_IMPLEMENTED_YET ); EvaluationPoint z( *transition, DX.getDim(), 0, DP.getDim(), DU.getDim(), DW.getDim() ); transition->AD_backward( DX, z ); DX = z.getX(); DP = z.getP(); DU = z.getU(); DW = z.getW(); return SUCCESSFUL_RETURN; }
Array StreamPeer::_get_data(int p_bytes) { Array ret; DVector<uint8_t> data; data.resize(p_bytes); if (data.size() != p_bytes) { ret.push_back(ERR_OUT_OF_MEMORY); ret.push_back(DVector<uint8_t>()); return ret; } DVector<uint8_t>::Write w = data.write(); Error err = get_data(&w[0], p_bytes); w = DVector<uint8_t>::Write(); ret.push_back(err); ret.push_back(data); return ret; }
DVector<Plane> Geometry::build_cylinder_planes(float p_radius, float p_height, int p_sides, Vector3::Axis p_axis) { DVector<Plane> planes; for (int i = 0; i < p_sides; i++) { Vector3 normal; normal[(p_axis + 1) % 3] = Math::cos(i * (2.0 * Math_PI) / p_sides); normal[(p_axis + 2) % 3] = Math::sin(i * (2.0 * Math_PI) / p_sides); planes.push_back(Plane(normal, p_radius)); } Vector3 axis; axis[p_axis] = 1.0; planes.push_back(Plane(axis, p_height * 0.5)); planes.push_back(Plane(-axis, p_height * 0.5)); return planes; }
Rect2 ConcavePolygonShape2D::get_rect() const { DVector<Vector2> s = get_segments(); int len=s.size(); if (len==0) return Rect2(); Rect2 rect; DVector<Vector2>::Read r = s.read(); for(int i=0;i<len;i++) { if (i==0) rect.pos=r[i]; else rect.expand_to(r[i]); } return rect; }
// FIXME parallellize TVal dot(const DVector<TVal, TIdx>& rhs) const { JWAssert(rhs.size() == size()); const auto& lhs = *this; TVal sum = 0; for (TIdx i = 0; i < this->size(); ++i) { sum += lhs[i] * rhs[i]; } return sum; }
void NavigationMesh::create_from_mesh(const Ref<Mesh>& p_mesh) { vertices=DVector<Vector3>(); clear_polygons(); for(int i=0;i<p_mesh->get_surface_count();i++) { if (p_mesh->surface_get_primitive_type(i)!=Mesh::PRIMITIVE_TRIANGLES) continue; Array arr = p_mesh->surface_get_arrays(i); DVector<Vector3> varr = arr[Mesh::ARRAY_VERTEX]; DVector<int> iarr = arr[Mesh::ARRAY_INDEX]; if (varr.size()==0 || iarr.size()==0) continue; int from = vertices.size(); vertices.append_array(varr); int rlen = iarr.size(); DVector<int>::Read r = iarr.read(); for(int j=0;j<rlen;j+=3) { Vector<int> vi; vi.resize(3); vi[0]=r[j+0]+from; vi[1]=r[j+1]+from; vi[2]=r[j+2]+from; add_polygon(vi); } } }
Ref<Mesh> Shape::get_debug_mesh() { if (debug_mesh_cache.is_valid()) return debug_mesh_cache; Vector<Vector3> lines = _gen_debug_mesh_lines(); debug_mesh_cache = Ref<Mesh>(memnew(Mesh)); if (!lines.empty()) { //make mesh DVector<Vector3> array; array.resize(lines.size()); { DVector<Vector3>::Write w=array.write(); for(int i=0;i<lines.size();i++) { w[i]=lines[i]; } } Array arr; arr.resize(Mesh::ARRAY_MAX); arr[Mesh::ARRAY_VERTEX]=array; SceneTree *st=OS::get_singleton()->get_main_loop()->cast_to<SceneTree>(); debug_mesh_cache->add_surface(Mesh::PRIMITIVE_LINES,arr); if (st) { debug_mesh_cache->surface_set_material(0,st->get_debug_collision_material()); } } return debug_mesh_cache; }
bool D_bjmak::getHashesAndData(int fd, DVector& dataBuffer, DVector& hashBuffer, int blockCount){ string hash; hash.reserve(26); for(int i = 0; i < blockCount; ++i){ char buf[bsize_]; int l = read(fd, buf, bsize_); //cout << " read " << l << " i = " << i << "blockCount = " << blockCount << endl; if (l == -1){ cout << " read errror " << strerror(errno) << endl; return false; } DVector hashstr; hash = Hash::sha1(buf, l); hashstr.push_back(hash.data(), hash.size()); hashstr.push_back(int64_to_byte(Hash::crc32(buf, l), 4)); hashstr.push_back(int64_to_byte(l, 2)); hashBuffer.push_back(hashstr); dataBuffer.push_back(buf, l); } return true; }
DVector D_bjmak::getDataBufferFromReply(DVector& reply, DVector& hashBuffer, DVector& dataBuffer){ int dataPos = 0; int blockCount = reply.size(); char* hashBufferPtr = hashBuffer.data(); char* dataBufferPtr = dataBuffer.data(); DVector res; res.reserve(blockCount*bsize_); for(int i = 0; i < blockCount; ++i){ int dataBlockSize = byte_to_int64((hashBufferPtr+i*(D_LENGTH_OF_HASH+2)+D_LENGTH_OF_HASH), 2); if (reply[i] == 1){ //cout << " 1 " << dataBlockSize << endl; res.push_back(dataBufferPtr+dataPos, dataBlockSize); }else //cout << " - " << (int)reply[i] << " " << dataBuffer.size() << " " << hashBuffer.size()<< " " << dataBlockSize << endl; dataPos += dataBlockSize; } //cout << endl; return move(res); }
returnValue Integrator::setForwardSeed( const int &order, const DVector &xSeed, const DVector &pSeed, const DVector &uSeed, const DVector &wSeed ){ int run1; if( rhs == 0 ) return ACADOERROR( RET_TRIVIAL_RHS ); DVector tmpX; DVector components = rhs->getDifferentialStateComponents(); dP = pSeed; dU = uSeed; dW = wSeed; if( xSeed.getDim() != 0 ){ tmpX.init( components.getDim() ); for( run1 = 0; run1 < (int) components.getDim(); run1++ ) tmpX(run1) = xSeed((int) components(run1)); } return setProtectedForwardSeed( tmpX, pSeed, uSeed, wSeed, order ); }
void AudioServer::sample_set_signed_data(RID p_sample, const DVector<float>& p_buffer) { SampleFormat format = sample_get_format(p_sample); ERR_EXPLAIN("IMA ADPCM is not supported."); ERR_FAIL_COND(format==SAMPLE_FORMAT_IMA_ADPCM); int len = p_buffer.size(); ERR_FAIL_COND( len == 0 ); DVector<uint8_t> data; DVector<uint8_t>::Write w; DVector<float>::Read r = p_buffer.read(); switch(format) { case SAMPLE_FORMAT_PCM8: { data.resize(len); w=data.write(); int8_t *samples8 = (int8_t*)w.ptr(); for(int i=0;i<len;i++) { float sample = Math::floor( r[i] * (1<<8) ); if (sample<-128) sample=-128; else if (sample>127) sample=127; samples8[i]=sample; } } break; case SAMPLE_FORMAT_PCM16: { data.resize(len*2); w=data.write(); int16_t *samples16 = (int16_t*)w.ptr(); for(int i=0;i<len;i++) { float sample = Math::floor( r[i] * (1<<16) ); if (sample<-32768) sample=-32768; else if (sample>32767) sample=32767; samples16[i]=sample; } } break; } w = DVector<uint8_t>::Write(); sample_set_data(p_sample,data); }
returnValue Integrator::getBackwardSensitivities( DVector &DX, DVector &DP , DVector &DU , DVector &DW , int order ) const{ int run2; returnValue returnvalue; DVector tmpX ( rhs->getDim() ); DX.setZero(); DP.setZero(); DU.setZero(); DW.setZero(); returnvalue = getProtectedBackwardSensitivities( tmpX, DP, DU, DW, order ); DVector components = rhs->getDifferentialStateComponents(); for( run2 = 0; run2 < (int) components.getDim(); run2++ ) DX((int) components(run2)) = tmpX(run2); for( run2 = 0; run2 < (int) dPb.getDim(); run2++ ) DP(run2) += dPb(run2); for( run2 = 0; run2 < (int) dUb.getDim(); run2++ ) DU(run2) += dUb(run2); for( run2 = 0; run2 < (int) dWb.getDim(); run2++ ) DW(run2) += dWb(run2); return returnvalue; }
OCP::OCP( const double &tStart_, const double &tEnd_, const DVector& _numSteps ) : MultiObjectiveFunctionality(), grid(new Grid()), objective(new Objective()), constraint(new Constraint()) { if( _numSteps.getDim() <= 0 ) ACADOERROR( RET_INVALID_ARGUMENTS ); DVector times( _numSteps.getDim()+1 ); times(0) = tStart_; double totalSteps = 0; uint i; for( i = 0; i < _numSteps.getDim(); i++ ) { totalSteps += _numSteps(i); } double h = (tEnd_ - tStart_)/totalSteps; for( i = 0; i < _numSteps.getDim(); i++ ) { times(i+1) = times(i) + h*_numSteps(i); } setupGrid( times ); modelData.setIntegrationGrid(*grid, totalSteps); }
DVector<uint8_t> _File::get_buffer(int p_length) const { DVector<uint8_t> data; ERR_FAIL_COND_V(!f,data); ERR_FAIL_COND_V(p_length<0,data); if (p_length==0) return data; Error err = data.resize(p_length); ERR_FAIL_COND_V(err!=OK,data); DVector<uint8_t>::Write w = data.write(); int len = f->get_buffer(&w[0],p_length); ERR_FAIL_COND_V( len < 0 , DVector<uint8_t>()); w = DVector<uint8_t>::Write(); if (len < p_length) data.resize(p_length); return data; }
Error HTTPClient::request_raw( Method p_method, const String& p_url, const Vector<String>& p_headers,const DVector<uint8_t>& p_body) { ERR_FAIL_INDEX_V(p_method,METHOD_MAX,ERR_INVALID_PARAMETER); ERR_FAIL_COND_V(status!=STATUS_CONNECTED,ERR_INVALID_PARAMETER); ERR_FAIL_COND_V(connection.is_null(),ERR_INVALID_DATA); static const char* _methods[METHOD_MAX]={ "GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS", "TRACE", "CONNECT"}; String request=String(_methods[p_method])+" "+p_url+" HTTP/1.1\r\n"; request+="Host: "+conn_host+":"+itos(conn_port)+"\r\n"; bool add_clen=p_body.size()>0; for(int i=0;i<p_headers.size();i++) { request+=p_headers[i]+"\r\n"; if (add_clen && p_headers[i].find("Content-Length:")==0) { add_clen=false; } } if (add_clen) { request+="Content-Length: "+itos(p_body.size())+"\r\n"; //should it add utf8 encoding? not sure } request+="\r\n"; CharString cs=request.utf8(); DVector<uint8_t> data; //Maybe this goes faster somehow? for(int i=0;i<cs.length();i++) { data.append( cs[i] ); } data.append_array( p_body ); DVector<uint8_t>::Read r = data.read(); Error err = connection->put_data(&r[0], data.size()); if (err) { close(); status=STATUS_CONNECTION_ERROR; return err; } status=STATUS_REQUESTING; return OK; }
DVector* NextCalculator::MultiplyVectors(std::vector<double> & first, std::vector<double> & second) { DVector coeffs_tmp; DVector *ret; if (first.size() < 1) { ret = new DVector(second); return ret; } if (second.size() < 1) { ret = new DVector(first); return ret; } if (second.size() != 2) throw NextException("Second vector's size is not 2."); ret = new DVector(); for (DVector::iterator first_ite = first.begin(); first_ite != first.end(); ++first_ite) for (DVector::iterator second_ite = second.begin(); second_ite != second.end(); ++second_ite) coeffs_tmp.push_back(*first_ite * *second_ite); ret->push_back(coeffs_tmp.data()[0]); double tmp = 0; for (int i = 1; i < coeffs_tmp.size()-1; i++) { tmp += coeffs_tmp.data()[i]; if (i % 2 == 0) { ret->push_back(tmp); tmp = 0; } } ret->push_back(coeffs_tmp.data()[coeffs_tmp.size()-1]); return ret; }
Array StreamPeer::_put_partial_data(const DVector<uint8_t> &p_data) { Array ret; int len = p_data.size(); if (len == 0) { ret.push_back(OK); ret.push_back(0); return ret; } DVector<uint8_t>::Read r = p_data.read(); int sent; Error err = put_partial_data(&r[0], len, sent); if (err != OK) { sent = 0; } ret.push_back(err); ret.push_back(sent); return ret; }
DVector<Face3> TriangleMesh::get_faces() const { if (!valid) return DVector<Face3>(); DVector<Face3> faces; int ts = triangles.size(); faces.resize(triangles.size()); DVector<Face3>::Write w=faces.write(); DVector<Triangle>::Read r = triangles.read(); DVector<Vector3>::Read rv = vertices.read(); for(int i=0;i<ts;i++) { for(int j=0;j<3;j++) { w[i].vertex[j]=rv[r[i].indices[j]]; } } w = DVector<Face3>::Write(); return faces; }
void ThetaOperator::returnScaledNormalDistribution( int level , double stdFactor , int scaleIndex , DVector& output){ /* --- Matlab code --- L = 6; N = -1:(1/2^L):1; Fakt = 5.3; V = tan(((pi/2) - 1/Fakt)*N); V1 = 3*atan( (N).^5+0.1*(N) ); V = V ./ max(V); V1 = V1 ./ max(V1); */ // first generate linear distribution int nrPoints = powerTwo[level] + 1; DVector tmpPoints(nrPoints); output.resize(nrPoints); for (int ii = 0 ; ii < nrPoints; ii++) tmpPoints[ii] = (double)(2*ii - powerTwo[level]) / (double)(powerTwo[level]); // ---- use different scaling formulas ----- switch (scaleIndex){ case 0:{ double internFactor = 1.0/7.0; // calculate grading for (int ii = 0 ; ii < nrPoints; ii++) output[ii] = tan( (FITOB_HALF_PI - internFactor)*tmpPoints[ii]); // do the scaling for (int ii = 0 ; ii < nrPoints; ii++) output[ii] = stdFactor * output[ii] / output[nrPoints-1]; break; } case 1:{ // do some prework in the formula for (int ii = 0 ; ii < nrPoints; ii++) output[ii] = pow(tmpPoints[ii],5) + 0.1 * tmpPoints[ii]; // calculate grading for (int ii = 0 ; ii < nrPoints; ii++) output[ii] = 3.0 * atan( output[ii] ); // do the scaling for (int ii = 0 ; ii < nrPoints; ii++) output[ii] = stdFactor * output[ii] / output[nrPoints-1]; break; } } }
void TileMap::_set_tile_data(const DVector<int>& p_data) { int c=p_data.size(); DVector<int>::Read r = p_data.read(); for(int i=0;i<c;i+=2) { const uint8_t *ptr=(const uint8_t*)&r[i]; uint8_t local[8]; for(int j=0;j<8;j++) local[j]=ptr[j]; #ifdef BIG_ENDIAN_ENABLED SWAP(local[0],local[3]); SWAP(local[1],local[2]); SWAP(local[4],local[7]); SWAP(local[5],local[6]); #endif int16_t x = decode_uint16(&local[0]); int16_t y = decode_uint16(&local[2]); uint32_t v = decode_uint32(&local[4]); bool flip_h = v&(1<<29); bool flip_v = v&(1<<30); bool transpose = v&(1<<31); v&=(1<<29)-1; // if (x<-20 || y <-20 || x>4000 || y>4000) // continue; set_cell(x,y,v,flip_h,flip_v,transpose); } }
DVector<Plane> Geometry::build_box_planes(const Vector3 &p_extents) { DVector<Plane> planes; planes.push_back(Plane(Vector3(1, 0, 0), p_extents.x)); planes.push_back(Plane(Vector3(-1, 0, 0), p_extents.x)); planes.push_back(Plane(Vector3(0, 1, 0), p_extents.y)); planes.push_back(Plane(Vector3(0, -1, 0), p_extents.y)); planes.push_back(Plane(Vector3(0, 0, 1), p_extents.z)); planes.push_back(Plane(Vector3(0, 0, -1), p_extents.z)); return planes; }
returnValue VariablesGrid::setVector( uint pointIdx, const DVector& _values ) { if ( pointIdx >= getNumPoints( ) ) return ACADOERROR( RET_INDEX_OUT_OF_BOUNDS ); if ( _values.getDim( ) != getNumRows( pointIdx ) ) return ACADOERROR( RET_VECTOR_DIMENSION_MISMATCH ); for( uint j=0; j<getNumRows( ); ++j ) operator()( pointIdx,j ) = _values( j ); return SUCCESSFUL_RETURN; }
DVector<Point2> Curve2D::bake(int p_subdivs) const { int pc = points.size(); DVector<Point2> ret; if (pc<2) return ret; ret.resize((pc-1)*p_subdivs+1); DVector<Point2>::Write w = ret.write(); const Point *r = points.ptr(); for(int i=0;i<pc;i++) { int ofs = pc*p_subdivs; int limit=(i==pc-1)?p_subdivs+1:p_subdivs; for(int j=0;j<limit;j++) { Vector2 p0 = r[i].pos; Vector2 p1 = p0+r[i].out; Vector2 p3 = r[i].pos; Vector2 p2 = p3+r[i].in; real_t t = j/(real_t)p_subdivs; w[ofs+j]=_bezier_interp(t,p0,p1,p2,p3); } } w = DVector<Point2>::Write(); return ret; }
returnValue Integrator::integrate( const Grid &t_ , const DVector &x0 , const DVector &xa , const DVector &p , const DVector &u , const DVector &w ){ int run1; returnValue returnvalue; if( rhs == 0 ) return ACADOERROR( RET_TRIVIAL_RHS ); DVector tmpX; DVector components = rhs->getDifferentialStateComponents(); const int N = components.getDim(); if( x0.getDim() != 0 ){ tmpX.init( components.getDim() ); for( run1 = 0; run1 < (int) components.getDim(); run1++ ) tmpX(run1) = x0((int) components(run1)); } // tmpX.print( "integrator x0" ); // u.print( "integrator u0" ); // p.print( "integrator p0" ); returnvalue = evaluate( tmpX, xa, p, u, w, t_ ); if( returnvalue != SUCCESSFUL_RETURN ) return returnvalue; xE.init(rhs->getDim()); xE.setZero(); DVector tmp(rhs->getDim()); getProtectedX(&tmp); for( run1 = 0; run1 < N; run1++ ) xE((int) components(run1)) = tmp(run1); for( run1 = N; run1 < N + ma; run1++ ) xE(run1) = tmp(run1); if( transition != 0 ) returnvalue = evaluateTransition( t_.getLastTime(), xE, xa, p, u, w ); return returnvalue; }
void SpriteFramesEditor::_file_load_request(const DVector<String> &p_path, int p_at_pos) { ERR_FAIL_COND(!frames->has_animation(edited_anim)); List<Ref<Texture> > resources; for (int i = 0; i < p_path.size(); i++) { Ref<Texture> resource; resource = ResourceLoader::load(p_path[i]); if (resource.is_null()) { dialog->set_text(TTR("ERROR: Couldn't load frame resource!")); dialog->set_title(TTR("Error!")); //dialog->get_cancel()->set_text("Close"); dialog->get_ok()->set_text(TTR("Close")); dialog->popup_centered_minsize(); return; ///beh should show an error i guess } resources.push_back(resource); } if (resources.empty()) { //print_line("added frames!"); return; } undo_redo->create_action(TTR("Add Frame")); int fc = frames->get_frame_count(edited_anim); int count = 0; for (List<Ref<Texture> >::Element *E = resources.front(); E; E = E->next()) { undo_redo->add_do_method(frames, "add_frame", edited_anim, E->get(), p_at_pos == -1 ? -1 : p_at_pos + count); undo_redo->add_undo_method(frames, "remove_frame", edited_anim, p_at_pos == -1 ? fc : p_at_pos); count++; } undo_redo->add_do_method(this, "_update_library"); undo_redo->add_undo_method(this, "_update_library"); undo_redo->commit_action(); //print_line("added frames!"); }