Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Sunday, July 20, 2008

Codes for Blending Functions of NURBS

Recently I have been interested in computing the value of the blending functions of NURBS. I provided two codes for computing the blending functions here. One uses the recursive method and the other uses the non-recursive method.

Recursive Method
pros : Easier to implement; No need for extra loops
cons : Need extra memory for function calls

Non-recursive Method
pros : Faster than recursive method; No need for ask for extra memory for function calls
cons : Need an array and need to use for-loop

Files : main.m(for testing) , BlendingU.m(recursive) ,
BlendingFun.m(non-recursive)

In the following, I will show how to obtain Np3 (p=1,2,3, ..., 8) and get the plot of the Blending function which will be shown in the end. Note one can change the 3 to 1 and 2 to see the difference. I mean to obtain Np1 and Np2.

main.m
clear
clc

%% Knot vector & Normalization
U=[0 0 0 1 2 3 4 4 5 5 5];
U=U/max(U);

%% Non-recursive
for u=0:0.0001:1
for p=1:1:8
N=BlendingFun(p,3,u,U);
subplot(211),plot(u,N);hold on
end
end
hold off

%% Recursive
for u=0:0.0001:1
for p=1:1:8
N=BlendingU(p,3,u,U);
subplot(212),plot(u,N);hold on
end
end



BlendingU.m
%% Recursive method to compute the blending function
%% Pros : Easier to implement; No need for extra loops
%% Cons : Need extra memory for function calls

function N=BlendingU(n1,pp,uu,Ubar)

if pp==1
if uu>Ubar(1,n1) & uu<Ubar(1,n1+1)
N=1;
else
N=0;
end
else
N1N=BlendingU(n1,pp-1,uu,Ubar);
N2N=BlendingU(n1+1,pp-1,uu,Ubar);
N=0;
if N1N~=0 & Ubar(1,n1+pp-1)-Ubar(1,n1)~=0
N1=(uu-Ubar(1,n1))/(Ubar(1,n1+pp-1)-Ubar(1,n1));
N=N1*N1N;
end
if N2N~=0 & Ubar(1,n1+pp)-Ubar(1,n1+1)~=0
N2=(Ubar(n1+pp)-uu)/(Ubar(1,n1+pp)-Ubar(1,n1+1));
N=N+N2*N2N;
end
end



BlendingFun.m
%% None-recursive method to compute the blending function
%% Only the associated k elements in U are used in this function
%% Need a 1 by (1+k)*k*0.5 array, say ARRAY in this function
%% Pros : Faster than recursive method; No need for ask for extra memory for
%% function calls
%% Cons : Need an array and need to use for-loop

function N=BlendingFun(i,k,u,U)

U=U(i:i+k);
ARRAY=zeros(1,(1+k)*k*0.5);

for ind=0:1:k-1
if u>=U(ind+1) & u<U(ind+1+1)
ARRAY(ind+1)=1;
end
end

layer=2;
pt=k+1;

while layer<=k
for x=0:1:(k+1)-layer-1
if U(x+1+layer-1)-U(x+1)~=0 & ARRAY(pt+x-(k+1-layer)-1)~=0
ARRAY(pt+x)=((u-U(x+1))/(U(x+1+layer-1)-U(x+1)))*ARRAY(pt+x-(k+1-layer)-1);
end
if U(x+1+1+layer-1)-U(x+1+1)~=0 & ARRAY(pt+x-(k+1-layer))~=0
ARRAY(pt+x)=ARRAY(pt+x)+((U(x+1+1+layer-1)-u)/(U(x+1+1+layer-1)-U(x+1+1)))*ARRAY(pt+x-(k+1-layer));
end
if x==(k+1)-layer-1
layer=layer+1;
pt=pt+(k+2)-layer;
end
end
end

%% Return the last element of ARRAY
N=ARRAY((1+k)*k*0.5);



Tuesday, July 15, 2008

ConsoleIn of C

Files: JohnIn.h, JohnIn.c

1. JohnIn.h
---------------------------------------------

/* JohnIn.h
*
* abstract data type
*
* Operator: JohnIn_int , JohnIn_char
*/


/*
* JohnIn_int
*/
extern int int_console(void);

/*
* JohnIn_cdouble
*/
extern double dou_console(void);

/*
* JohnIn_char
*/
extern char char_console(void);



2. JohnIn.c
---------------------------------------------

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include"JohnIn.h"

int int_console(void){
char temp[10];
int value1, test1;

scanf("%s", temp);
value1=atoi(temp);
sscanf(temp, "%d", &test1);
printf("%s", temp);

while(value1!=test1){
printf("\nInput is in wrong format.\n");
printf("Input number with at most 10 digits.\n");
printf("Minus sign is okay.\n");
printf("reenter an integer ---> ");
scanf("%s", temp);
value1=atoi(temp);
sscanf(temp, "%d", &test1);
}
return(value1);
}

double dou_console(void){
char temp[15];
double value2, test2;

scanf("%s", temp);
value2=atof(temp);
sscanf(temp, "%lf", &test2);

while(value2!=test2){
printf("\nInput is in wrong format.\n");
printf("Input number with at most 15 digits.\n");
printf("Minus sign is okay.\n");
printf("reenter an double ---> ");
scanf("%s", temp);
value2=atoi(temp);
sscanf(temp, "%lf", &test2);
}
return(value2);
}

char char_console(void){
char temp[2];

scanf("%s", temp);

while(strlen(temp)!=1){
printf("\nInput is in wrong format.\n");
printf("Input only one char.\n");
printf("reenter a char ---> ");
scanf("%s", temp);
}
return(temp[0]);
}

Friday, June 15, 2007

十六進位轉換(內建)

我把那個function貼過來
到今天我才知道,當你叫一個printf時候
他真的去call很多function的說 好複雜呀
我有把轉換的地方標出來了

static char *number(char *str, long num, int base, int size, int precision, int type)
{
char c, sign, tmp[66];
char *dig = digits;
int i;

if (type & LARGE) dig = upper_digits;
if (type & LEFT) type &= ~ZEROPAD;
if (base < 2 || base > 36) return 0;

c = (type & ZEROPAD) ? '0' : ' ';
sign = 0;
if (type & SIGN)
{
if (num < 0)
{
sign = '-';
num = -num;
size--;
}
else if (type & PLUS)
{
sign = '+';
size--;
}
else if (type & SPACE)
{
sign = ' ';
size--;
}
}

if (type & SPECIAL)
{
if (base == 16)
size -= 2;
else if (base == 8)
size--;
}

i = 0;

if (num == 0)
tmp[i++] = '0';
else
{
while (num != 0)
{
tmp[i++] = dig[((unsigned long) num) % (unsigned) base];
num = ((unsigned long) num) / (unsigned) base;
}
}

if (i > precision) precision = i;
size -= precision;
if (!(type & (ZEROPAD | LEFT))) while (size-- > 0) *str++ = ' ';
if (sign) *str++ = sign;

if (type & SPECIAL)
{
if (base == 8)
*str++ = '0';
else if (base == 16)
{
*str++ = '0';
*str++ = digits[33];
}
}

if (!(type & LEFT)) while (size-- > 0) *str++ = c;
while (i < precision--) *str++ = '0';
while (i-- > 0) *str++ = tmp[i];
while (size-- > 0) *str++ = ' ';

return str;
}

十六進位程式(好久前的東西)

#include<stdio.h>
#include<string.h>

int main(){
char *digits[]={"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
char output[32], buffer[32];
int input, exit, i;

printf("Please enter a decimal number : ");
scanf("%d", &input);
while(input!=0){
strcat(output,digits[input & 15]);
input>>=4;
}

for(i=strlen(output)-1;i>=0;i--)
printf("%c", output[i]);

printf("\n");
scanf("%d", &exit);

return(0);
}

========================
以上程式是上禮拜寫的
是學弟的作業 我ㄧ看到就去翻翻java的寫法
然後把它改成c 因為轉成十六進位的程式
java的Integer.java就有了 所以...
程式的重點在do-while迴圈
每進去一次回圈要執行三個步驟
每進去一次就可以算出一位
所以可以算算大致上執行了幾個步驟...

這個程式的寫法也可以有很多種
最近寫程式的感覺是
有時候用太多class或function會造成overhead
所以效率就會差很多了...