// Problem 1 void ExSolvePoisson(int xRes, int yRes, int _iterations, double _accuracy, double* field, double* b) { // double dt = fluid.dt; double *p_old = new double[xRes*yRes]; double *p_new = new double[xRes*yRes]; for (int y = 0; y < yRes; y++) for (int x = 0; x < xRes; x++){ p_old[x + y*xRes] = 0.0; p_new[x + y*xRes] = GETF(x, y); // printf("%f\n", p_new[x + y*xRes]); } for (int iter = 0; iter < _iterations; ++iter) { double *p_old_ = p_old; p_old = p_new; p_new = p_old_; double residual = 0; // note that the boundaries are handles by the framework, so you iterations should be similar to: for (int y = 1; y < yRes - 1; y++) { for (int x = 1; x < xRes - 1; x++) { double d = GETB(x, y); double p_old_FD = p_old[x+1 + y*xRes] + p_old[x + (y+1)*xRes] + p_old[x-1 + y*xRes] + p_old[x + (y-1)*xRes]; p_new[x + y*xRes] = 0.25 * ( d + p_old_FD ); double Ap_new = 4.0 * p_new[x + y*xRes] - (p_new[x+1 + y*xRes] + p_new[x + (y+1)*xRes] + p_new[x-1 + y*xRes] + p_new[x + (y-1)*xRes]); residual += fabs(d - Ap_new); // printf("b = %f\n", p_old[x+y*xRes]); } } residual /= (double)(xRes*yRes); //for your debugging, and ours, please add these prints after every iteration if(iter==_iterations-1) printf("Pressure solver: iter=%d , res=%f \n",iter, residual); if(residual<_accuracy) { printf("Pressure solver: iter=%d , converged \n",iter,residual); break; // optional } } for (int y = 1; y < yRes - 1; y++) for (int x = 1; x < xRes - 1; x++){ GETF(x, y) = p_new[x + y*xRes]; } }
int usys_listen(int *err, uuprocess_t *u, int fd, int backlog) { (void)backlog; if( u == 0 ) { *err = ENOTSOCK; // TODO correct? return -1; } CHECK_FD(fd); struct uufile *f = GETF(fd); struct uusocket *us = f->impl; if( ! (f->flags & (UU_FILE_FLAG_NET|UU_FILE_FLAG_TCP))) { *err = ENOTSOCK; return -1; } if( tcp_listen(us->prot_data) ) *err = EISCONN; return *err ? -1 : 0; }
ssize_t usys_sendmsg(int *err, uuprocess_t *u, int fd, const struct msghdr *msg, int flags) { (void) msg; if( u == 0 ) { *err = ENOTSOCK; // TODO correct? return -1; } CHECK_FD(fd); struct uufile *f = GETF(fd); int len = 0; struct uusocket *us = f->impl; (void) us; // TODO implement me if( ! (f->flags & UU_FILE_FLAG_NET)) { *err = ENOTSOCK; return -1; } if( flags ) SHOW_ERROR( 0, "I don't know this flag %d", flags ); *err = ENOSYS; return *err ? -1 : len; }
int usys_getsockopt(int *err, uuprocess_t *u, int fd, int level, int optname, void *optval, socklen_t *optlen) { (void) optname; (void) optlen; (void) optval; if( u == 0 ) { *err = ENOTSOCK; // TODO correct? return -1; } CHECK_FD(fd); struct uufile *f = GETF(fd); struct uusocket *us = f->impl; (void) us; if( ! (f->flags & UU_FILE_FLAG_NET)) { *err = ENOTSOCK; return -1; } if( level != SOL_SOCKET ) { *err = ENOPROTOOPT; return -1; } *err = ENOPROTOOPT; return *err ? -1 : 0; }
ssize_t usys_recvfrom(int *err, uuprocess_t *u, int fd, void *buf, size_t buflen, int flags, struct sockaddr *from, socklen_t *fromlen) { if( u == 0 ) { *err = ENOTSOCK; // TODO correct? return -1; } CHECK_FD(fd); struct uufile *f = GETF(fd); struct uusocket *us = f->impl; if( *fromlen < (int)sizeof(struct sockaddr_in) ) { *err = EINVAL; return -1; } if( ! (f->flags & UU_FILE_FLAG_NET)) { *err = ENOTSOCK; return -1; } if( flags ) SHOW_ERROR( 0, "I don't know this flag %d", flags ); i4sockaddr tmp_addr; //struct sockaddr_in *sfrom = (struct sockaddr_in *)from; // FIXME TODO ERR allways times out in 5 sec int len = udp_recvfrom( us->prot_data, buf, buflen, &tmp_addr, SOCK_FLAG_TIMEOUT, 5000000L ); if( len < 0 ) { SHOW_ERROR( 7, "ret = %d", len ); *err = -len; goto ret; } SHOW_FLOW( 8, "flags %x", flags ); /* SHOW_FLOW( 7, "port %d, ip %s", tmp_addr.port, inet_itoa(htonl(NETADDR_TO_IPV4(tmp_addr.addr))) ); sfrom->sin_port = htons(tmp_addr.port); sfrom->sin_addr.s_addr = htonl(NETADDR_TO_IPV4(tmp_addr.addr)); sfrom->sin_family = PF_INET; sfrom->sin_len = sizeof(struct sockaddr_in); *fromlen = sfrom->sin_len; */ errno_t rc = sockaddr_int2unix( from, fromlen, &tmp_addr ); if( rc ) *fromlen = 0; ret: return *err ? -1 : len; }
int usys_setsockopt(int *err, uuprocess_t *u, int fd, int level, int optname, const void *optval, socklen_t optlen) { if( u == 0 ) { *err = ENOTSOCK; // TODO correct? return -1; } CHECK_FD(fd); struct uufile *f = GETF(fd); struct uusocket *us = f->impl; if( ! (f->flags & UU_FILE_FLAG_NET) ) { *err = ENOTSOCK; return -1; } if( level != SOL_SOCKET ) { *err = ENOPROTOOPT; return -1; } if( (optname > 0x1000) && (optname < 0x100F) ) { switch(optname) { } } else { // Flags, one bit if(optlen != sizeof(int)) { *err = ENOPROTOOPT; return -1; } int set = *(int *)optval; if( set ) us->options |= optname; else us->options &= ~optname; // TODO this 'us->options' is not connected to something in IP stack! // so return err yet //return 0; } *err = ENOPROTOOPT; return *err ? -1 : 0; }
ssize_t usys_sendto(int *err, uuprocess_t *u, int fd, const void *buf, size_t buflen, int flags, const struct sockaddr *to, socklen_t tolen) { if( u == 0 ) { *err = ENOTSOCK; // TODO correct? return -1; } CHECK_FD(fd); struct uufile *f = GETF(fd); struct uusocket *us = f->impl; (void) us; if( tolen < (int)sizeof(struct sockaddr_in) ) { *err = EINVAL; return -1; } if( ! (f->flags & UU_FILE_FLAG_NET)) { *err = ENOTSOCK; return -1; } if( flags ) SHOW_ERROR( 0, "I don't know this flag %d", flags ); /* struct sockaddr_in *sto = (struct sockaddr_in *)to; if( sto->sin_family != PF_INET ) SHOW_ERROR0( 0, "not inet addr?"); */ i4sockaddr tmp_addr; errno_t rc = sockaddr_unix2int( &tmp_addr, to, tolen ); if( rc ) { *err = rc; return -1; } //tmp_addr.port = ntohs(sto->sin_port); //NETADDR_TO_IPV4(tmp_addr.addr) = ntohl(sto->sin_addr.s_addr); SHOW_FLOW( 8, "flags %x", flags ); //SHOW_FLOW( 7, "port %d, ip %s", tmp_addr.port, inet_itoa(htonl(NETADDR_TO_IPV4(tmp_addr.addr))) ); int ret = udp_sendto( us->prot_data, buf, buflen, &tmp_addr); if( ret < 0 ) *err = -ret; return *err ? -1 : ret; }
void unsanitise_value(char *out, unsigned *out_len_r, const char *in) { const char *ip; char *op; unsigned c; int n; for (ip = in, op = out; (c = *ip++); *op++ = c) { if (c == '\\') { c = *ip++; #define GETF(f) do { \ n = 0; \ sscanf(ip, f "%n", &c, &n); \ ip += n; \ } while (0) switch (c) { case 't': c= '\t'; break; case 'n': c= '\n'; break; case 'r': c= '\r'; break; case '\\': c= '\\'; break; case 'x': GETF("%2x"); break; case '0': case '4': case '1': case '5': case '2': case '6': case '3': case '7': --ip; GETF("%3o"); break; case 0: --ip; break; default:; } #undef GETF } } *op = 0; if (out_len_r) *out_len_r = op - out; }
int usys_connect(int *err, uuprocess_t *u, int fd, const struct sockaddr *_ia, socklen_t addrlen) { if( addrlen < (int)sizeof(struct sockaddr_in) ) { *err = EINVAL; return -1; } if( u == 0 ) { *err = ENOTSOCK; // TODO correct? return -1; } CHECK_FD(fd); struct uufile *f = GETF(fd); struct uusocket *us = f->impl; //if( (u == 0) || ! (f->flags & (UU_FILE_FLAG_NET|UU_FILE_FLAG_TCP))) if( (! (f->flags & UU_FILE_FLAG_NET)) || (! (f->flags & UU_FILE_FLAG_TCP)) ) { *err = ENOTSOCK; return -1; } //struct sockaddr_in *ia = (void *)_ia; i4sockaddr tmp_addr; errno_t rc = sockaddr_unix2int( &tmp_addr, _ia, addrlen ); if( rc ) { *err = rc; return -1; } /* tmp_addr.port = ia->sin_port; NETADDR_TO_IPV4(tmp_addr.addr) = ia->sin_addr.s_addr; if( ia->sin_family != PF_INET ) SHOW_ERROR0( 0, "not inet addr?"); */ int tret = tcp_connect( us->prot_data, &tmp_addr ); // TODO ret code! if( tret ) *err = ECONNREFUSED; return tret ? -1 : 0; }
int usys_getpeername(int *err, uuprocess_t *u, int fd, struct sockaddr *name, socklen_t *namelen) { if( u == 0 ) { *err = ENOTSOCK; // TODO correct? return -1; } CHECK_FD(fd); struct uufile *f = GETF(fd); struct uusocket *us = f->impl; if( ! (f->flags & UU_FILE_FLAG_NET)) { *err = ENOTSOCK; return -1; } if( ! (f->flags & UU_FILE_FLAG_TCP) ) { *err = ENOTCONN; // Right? return -1; } /* if( (unsigned)(*namelen) < sizeof(struct sockaddr) ) { *err = EINVAL; return -1; } *namelen = sizeof(struct sockaddr); */ i4sockaddr tmp_addr; if( tcp_getpeername(us->prot_data, &tmp_addr) ) { *err = ENOTCONN; return -1; } return sockaddr_int2unix( name, namelen, &tmp_addr ); }
int usys_getsockname(int *err, uuprocess_t *u, int fd, struct sockaddr *name, socklen_t *namelen) { if( u == 0 ) { *err = ENOTSOCK; // TODO correct? return -1; } CHECK_FD(fd); struct uufile *f = GETF(fd); struct uusocket *us = f->impl; if( ! (f->flags & UU_FILE_FLAG_NET)) { *err = ENOTSOCK; return -1; } sockaddr_int2unix( name, namelen, &us->addr ); //*namelen = sizeof(us->addr); //*name = us->addr; return 0; }
int usys_accept(int *err, uuprocess_t *u, int fd, struct sockaddr *acc_addr, socklen_t *addrlen) { if( u == 0 ) { *err = ENOTSOCK; // TODO correct? return -1; } CHECK_FD(fd); struct uufile *f = GETF(fd); struct uusocket *us = f->impl; // todo require UU_FILE_FLAG_ACCEPTABLE if( ! (f->flags & (UU_FILE_FLAG_NET|UU_FILE_FLAG_TCP)) ) { *err = ENOTSOCK; return -1; } //us->addr = my_addr; void *new_socket = NULL; i4sockaddr tmp_addr; int pe = tcp_accept(us->prot_data, &tmp_addr, &new_socket); if( *addrlen >= (int)sizeof(struct sockaddr_in) ) { /* struct sockaddr_in ia; ia.sin_len = sizeof(struct sockaddr_in); ia.sin_port = tmp_addr.port; ia.sin_addr.s_addr = NETADDR_TO_IPV4(tmp_addr.addr); ia.sin_family = PF_INET; *((struct sockaddr_in *)acc_addr) = ia; *addrlen = sizeof(struct sockaddr_in); */ if( sockaddr_int2unix( acc_addr, addrlen, &tmp_addr ) ) *addrlen = 0; } else *addrlen = 0; // TODO translate! if( pe ) { *err = ECONNABORTED; return -1; } struct uusocket *rus = calloc(1, sizeof(struct uusocket)); if(rus == 0) { tcp_close( new_socket ); *err = ENOMEM; return -1; } uufile_t *rf = create_uufile(); assert(f); rf->ops = &tcpfs_fops; rf->pos = 0; rf->fs = &tcp_fs; rf->impl = rus; rf->flags = UU_FILE_FLAG_NET|UU_FILE_FLAG_TCP; int rfd = uu_find_fd( u, rf ); if( rfd < 0 ) { tcp_close( new_socket ); unlink_uufile( f ); free( us ); *err = EMFILE; return -1; } return *err ? -1 : rfd; }
int usys_bind(int *err, uuprocess_t *u, int fd, const struct sockaddr *addr, socklen_t addrlen) { if( u == 0 ) { *err = ENOTSOCK; // TODO correct? return -1; } CHECK_FD(fd); struct uufile *f = GETF(fd); struct uusocket *us = f->impl; if( ! (f->flags & UU_FILE_FLAG_NET) ) { *err = ENOTSOCK; return -1; } /* if( (addrlen < my_addr->sa_len) || (addrlen < (int)sizeof(struct sockaddr_in)) ) { *err = EINVAL; return -1; } */ //us->addr = *my_addr; errno_t rc = sockaddr_unix2int( &us->addr, addr, addrlen ); if( rc ) { *err = rc; return -1; } int pe; if( f->flags & UU_FILE_FLAG_TCP ) { pe = tcp_bind(us->prot_data, &us->addr); } else if( f->flags & UU_FILE_FLAG_UDP ) { pe = udp_bind(us->prot_data, &us->addr); } else { *err = ENOPROTOOPT; return -1; } // TODO translate! if( pe ) *err = EADDRINUSE; return *err ? -1 : 0; }
static void ConvertRuneAnimations(UMeshAnimation &Anim, const TArray<RJoint> &Bones, const TArray<FRSkelAnimSeq> &Seqs) { guard(ConvertRuneAnimations); int i, j; int numBones = Bones.Num(); // create RefBones Anim.RefBones.Empty(Bones.Num()); for (i = 0; i < Bones.Num(); i++) { const RJoint &SB = Bones[i]; FNamedBone *B = new(Anim.RefBones) FNamedBone; B->Name = SB.name; B->Flags = 0; B->ParentIndex = SB.parent; } // create AnimSeqs Anim.AnimSeqs.Empty(Seqs.Num()); Anim.Moves.Empty(Seqs.Num()); for (i = 0; i < Seqs.Num(); i++) { // create FMeshAnimSeq const FRSkelAnimSeq &SS = Seqs[i]; FMeshAnimSeq *S = new(Anim.AnimSeqs) FMeshAnimSeq; S->Name = SS.Name; CopyArray(S->Groups, SS.Groups); S->StartFrame = 0; S->NumFrames = SS.NumFrames; S->Rate = SS.Rate; //?? S->Notifys // create MotionChunk MotionChunk *M = new(Anim.Moves) MotionChunk; M->TrackTime = SS.NumFrames; // dummy bone remap M->AnimTracks.Empty(numBones); // convert animation data const byte *data = &SS.animdata[0]; for (j = 0; j < numBones; j++) { // prepare AnalogTrack AnalogTrack *A = new(M->AnimTracks) AnalogTrack; A->KeyQuat.Empty(SS.NumFrames); A->KeyPos.Empty(SS.NumFrames); A->KeyTime.Empty(SS.NumFrames); } for (int frame = 0; frame < SS.NumFrames; frame++) { for (int joint = 0; joint < numBones; joint++) { AnalogTrack &A = M->AnimTracks[joint]; FVector pos, scale; pos.Set(0, 0, 0); scale.Set(1, 1, 1); FRotator rot; rot.Set(0, 0, 0); byte f = *data++; int16 d; #define GET d = data[0] + (data[1] << 8); data += 2; #define GETF(v) { GET; v = (float)d / 256.0f; } #define GETI(v) { GET; v = d; } // decode position if (f & 1) GETF(pos.X); if (f & 2) GETF(pos.Y); if (f & 4) GETF(pos.Z); // decode scale if (f & 8) { GETF(scale.X); GETF(scale.Z); } if (f & 0x10) GETF(scale.Y); // decode rotation if (f & 0x20) GETI(rot.Pitch); if (f & 0x40) GETI(rot.Yaw); if (f & 0x80) GETI(rot.Roll); #undef GET #undef GETF #undef GETI A.KeyQuat.Add(EulerToQuat(rot)); A.KeyPos.Add(pos); //?? notify about scale!=(1,1,1) } } assert(data == &SS.animdata[0] + SS.animdata.Num()); } unguard; }